473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1065
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1811
by: Carol Carrot | last post by:
Greetings. I've never seen a more complicated, verbose pile of hogwash than the "deliverd by mailman" mailing list program. Still I have to use it. I'm trying to get a header to get...
4
4615
by: Dave | last post by:
Hello all, For purposes of understanding the language better, I tried to determine the offset of a struct member by using pointers to members (instead of something such as the offsetof macro). ...
8
1294
by: Kev | last post by:
Hi, lets see if I can explain this right. :o) Lets say I wish to change several variables nested multiple classes deep. The usual way I figure is to just say "class#.struct.a (b,c...) =...
3
5050
by: Timo | last post by:
I am trying to pass a pointer to an array of structures to function, but I have some problems. I am using MS Visual C++ 6.0, but I think this is more of a C-problem than Windows programming...
40
5649
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
204
12883
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
4
1354
by: Sahil Malik | last post by:
Lets say .. I get a string .. "System.Windows.Forms.TextBox" Can I instantiate an instance of that .. given that the ONLY information I have is that string above i.e. classname? To make...
14
2518
by: povoação | last post by:
can´t understand why the program above not works. thanks all #include <stdio.h> #include <stdlib.h> #define SIZE 3 struct peoples
1
2403
by: gregory.lielens | last post by:
Hello, We are currently writing python bindings to an existing C++ library, and we encountered a problem that some of you may have solved (or that has found unsolvable :( ): A C++ class...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.