Writie classes of some algorithms

V

van

Writing classes of some algorithms, some parameters needs to be passed into
the classes, the actual algorithms are contained in member functions of
classes.

What I am thinking is having some ordinary member variables as parameters
for algorithms, and member functions as "static". Since "static" member
function cannot access ordinary member variables, it is logic to make the
latter as "static" as well.

I am very curious about how you'd like to design such kind of classes.

Thanks for your sharing!
 
J

JKop

van posted:
Writing classes of some algorithms, some parameters needs to be passed
into the classes, the actual algorithms are contained in member
functions of classes.

What I am thinking is having some ordinary member variables as
parameters for algorithms, and member functions as "static".

Why are you going with "static" if you have ordinary member variables?
Since "static" member function cannot access ordinary member variables, it
is logic to make the latter as "static" as well.

I am very curious about how you'd like to design such kind of classes.

Thanks for your sharing!

Okkayyyyy... very little information, but I'll give it a go.

Let's say that the parameters for the algorithm are:

a string
a number
a boolean

So far we have:

class Ranger
{
public:

const char* name;
int id;
bool is_alive;
};



Now with some algorithmic functions:


class Ranger
{
public:

const char* name;
int id;
bool is_alive:

int WhatYearWasBorn();
unsigned char AmountOfChildren();
bool WillBeAliveTomorrow();
};

int main()
{
Ranger frankie;

frankie.name = "Francis Duffy";
frankie.id = 2256;
frankie.is_alive = true;

//Ofcourse you'd have a constructor for the above

//Now here comes algorithmic functions that work
//with the member data:

frankie.WhatYearWasBorn();
frankie.AmountOfChildren();
frankie.WillBeAliveTomorrow();


};


Did that clear anything up at all?


-JKop
 
V

Victor Bazarov

van said:
Writing classes of some algorithms, some parameters needs to be passed into
the classes, the actual algorithms are contained in member functions of
classes.

What I am thinking is having some ordinary member variables as parameters
for algorithms, and member functions as "static". Since "static" member
function cannot access ordinary member variables, it is logic to make the
latter as "static" as well.

I am very curious about how you'd like to design such kind of classes.

The usual approach to doing what you've described (IIUIC) is to write
a class that will be constructed with the necessary arguments and then
its overloaded operator() (operator function call) should implement the
actual algorithm. Pass additional arguments to the operator() as needed.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top