468,511 Members | 1,691 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

is this possible - pointer to a classtype, that differs?

Hi,

I have a class:

class Aclass
{
void Hello();
}

AClass::Hello()
{
printf ("Hello\r\n");
}

How can i make different AClass:Hello() functions - without creating a new
classtype ?

The reason i ask, is i have a list of classes, which all should contain a
pointer to the same classtype - but even though the functions are the same,
the code needs to be different.

I were wondering about something like "Friend" or similar ?
Jul 23 '05 #1
2 1008
mandatory wrote:
How can i make different AClass:Hello() functions - without creating a new
classtype ?


Have the AClass::Hello() type function "call back" to the original
callers. These objects "know" what they are and can do the right thing
if you need unique behavior for each.
Jul 23 '05 #2
On 2005-03-20, mandatory <ok*****@nptklooohs.ru> wrote:
Hi,

I have a class:

class Aclass
{
void Hello();
}

AClass::Hello()
{
printf ("Hello\r\n");
}

How can i make different AClass:Hello() functions - without creating a new
classtype ?

The reason i ask, is i have a list of classes,
C++ doesn't support "lists of classes", though there are many idioms that do
this. If you're using such an idiom, you should say which. If you mean "list
of objects", it's important to use the correct terminology ("list of classes"
means something completely different)
which all should contain a
pointer to the same classtype - but even though the functions are the same,
the code needs to be different.

I were wondering about something like "Friend" or similar ?


Assuming you mean list of objects:

one way is to use polymorphism:

class AClass {
public:
virtual void hello() const = 0;
};

class AClass1 : public AClass {
public:
void hello() const { printf("hello\n"); }
};

class AClass2 : public AClass {
public:
void hello() const { printf("HELLO\n"); }
};

list<smart_pointer<AClass> > x;
x.push_back(new AClass1);
x.push_back(new AClass2);

for (list<smart_pointer<AClass> >::iterator i =
x.begin(); i!=x.end(); ++i)
(*i)->hello();

Approach 2: callbacks

typedef void (*hello_function_t) ();

class AClass {
private:
hello_function_t f;
public:
AClass (hello_function_t f) : f(f) {}
};

Approach 3: callbacks via function objects:

class HelloFunction {
public:
virtual operator() const = 0;
};

class AClass {
private:
HelloFunction* f;
public:
...

then get different behaviour by deriving from HelloFunction

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Carol Carrot | last post: by
4 posts views Thread by Dave | last post: by
8 posts views Thread by Kev | last post: by
40 posts views Thread by Ike Naar | last post: by
204 posts views Thread by Alexei A. Frounze | last post: by
4 posts views Thread by Sahil Malik | last post: by
14 posts views Thread by povoação | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.