473,242 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

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 1062
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
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
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
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
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
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
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.