473,226 Members | 1,697 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,226 software developers and data experts.

calling virtual function results in calling function of base class...

Hi,
after browsing FAQ and archive for a while I decided that the following
is a legal question.

Consider this:

Class Base
{
public:
Base() {}

Base(A* a, B* b);

virtual A* foo(P* p, S& s); // using myA and myB
protected:
A* myA;
B* myB;
};

class Der : public Base
{
public:
Der(O* o) : Base(), myO(o) {}
A* foo(P* p; S& s); // not using myA and myB
private:
O* myO;
};

class Bogus
{
public:
Bogus(Base* b) : myBase(b) {}

void bar()
{
...
myBase->foo(p,s);
...
}
private:
Base* myBase;
};

int main(/*args*/)
{
...
Der der(someO);
Bogus* bogus = new Bogus(der);
...
bogus->bar();
...
};

Executing this results in Base::foo being called instead of Der::foo
(what I would expect) ....
What am I doing wrong ?
Is that because cstr of Der does not initialize any mebers of Base ?
How could I possibly avoid that or get the behaviour I need ?
Pleas help!!!

Regards,

Andreas Lagemann
Jul 22 '05 #1
8 1593

"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41***********************@news.freenet.de...
Hi,
after browsing FAQ and archive for a while I decided that the following
is a legal question.

Consider this:

Class Base
{
public:
Base() {}

Base(A* a, B* b);
This won't compile because types 'A' and 'B' have
not been declared at this point.

virtual A* foo(P* p, S& s); // using myA and myB
None of types 'A', 'P', or 'S' have been declared
at this point.
protected:
A* myA;
B* myB;
};

class Der : public Base
{
public:
Der(O* o) : Base(),
This will happen automatically, you need not specify it.
myO(o) {}
A* foo(P* p; S& s); // not using myA and myB
None of the types 'A', 'P', or 'S' have been declared
at this point.
private:
O* myO;
};

class Bogus
{
public:
Bogus(Base* b) : myBase(b) {}

void bar()
{
...
myBase->foo(p,s);
...
}
private:
Base* myBase;
};

int main(/*args*/)
{
...
Der der(someO);
Bogus* bogus = new Bogus(der);
...
bogus->bar();
...
};

Executing this results in Base::foo being called instead of Der::foo
(what I would expect) ....
What am I doing wrong ?
Misunderstanding. 'Base::foo()' is being called because you
supplied a pointer to a 'Base' object. If you want 'Der::foo'
to be called, you need to suppy the address of (or a reference to)
a type 'Der' object.
Is that because cstr of Der does not initialize any mebers of Base ?
No.
How could I possibly avoid that or get the behaviour I need ?

If you want to call a function of 'Der' via a pointer to its
base, intialize the base pointer with the address of a 'Der'
object.

-Mike
Jul 22 '05 #2
Mike Wahler schrieb:
"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41***********************@news.freenet.de...
Hi,
after browsing FAQ and archive for a while I decided that the following
is a legal question.

Consider this:

Class Base
{
public:
Base() {}

Base(A* a, B* b);

This won't compile because types 'A' and 'B' have
not been declared at this point.

virtual A* foo(P* p, S& s); // using myA and myB

None of types 'A', 'P', or 'S' have been declared
at this point.

Yes, I just wanted to keep the posting short, the exact form of A, B, P,
S is irrelevant to the question (I think), Sorry for not mentioning that.
protected:
A* myA;
B* myB;
};

class Der : public Base
{
public:
Der(O* o) :


Base(),

This will happen automatically, you need not specify it.

I know that, I just wanted to stress the fact that I call the std. cstr
at that point.
myO(o) {}
A* foo(P* p; S& s); // not using myA and myB

None of the types 'A', 'P', or 'S' have been declared
at this point.

see above
private:
O* myO;
};

class Bogus
{
public:
Bogus(Base* b) : myBase(b) {}

void bar()
{
...
myBase->foo(p,s);
...
}
private:
Base* myBase;
};

int main(/*args*/)
{
...
Der der(someO);

[edit] Bogus* bogus = new Bogus(&der);
^^^^
that was what I meant ...
...
bogus->bar();
...
};

Executing this results in Base::foo being called instead of Der::foo
(what I would expect) ....
What am I doing wrong ?

Misunderstanding. 'Base::foo()' is being called because you
supplied a pointer to a 'Base' object. If you want 'Der::foo'
to be called, you need to suppy the address of (or a reference to)
a type 'Der' object.

Sorry but as far as I understand, I supplied a pointer (sorry for the
typo) to Der (i.e. &der) when initializing Bogus. Therefore if I call
bogus->bar() Der::foo should be called, shouldn´t it ?
Is that because cstr of Der does not initialize any mebers of Base ?

No.

How could I possibly avoid that or get the behaviour I need ?


If you want to call a function of 'Der' via a pointer to its
base, intialize the base pointer with the address of a 'Der'
object.


I thought that was what I have done !? (see above)

Perhaps another Question for clarification:
I want to be able to call a specialized versions of a certain function
without knowing the exact type of specialization at compile time. That
is what I thought virtual functions were intended for ...

Can please anyone point out, how I could achieve such a behaviour ?

Regards and thanks for the answer so far,

Andreas
Jul 22 '05 #3

"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41**********************@news.freenet.de...
Mike Wahler schrieb:
"Andreas Lagemann" <an**************@freenet.de> wrote in message Misunderstanding. 'Base::foo()' is being called because you
supplied a pointer to a 'Base' object. If you want 'Der::foo'
to be called, you need to suppy the address of (or a reference to)
a type 'Der' object.
Sorry but as far as I understand, I supplied a pointer (sorry for the
typo) to Der (i.e. &der) when initializing Bogus. Therefore if I call
bogus->bar() Der::foo should be called, shouldn´t it ?


'bogus' is not a pointer to your base class. It's a pointer
to a type 'Bogus' object.
Perhaps another Question for clarification:
I want to be able to call a specialized versions of a certain function
without knowing the exact type of specialization at compile time. That
is what I thought virtual functions were intended for ...

Can please anyone point out, how I could achieve such a behaviour ?


Assign the address of a derived object to a pointer to an object
of its base class.

#include <iostream>

class Base
{
public:
virtual void f() { std::cout << "base\n"; }
};

class Derived : public Base
{
public:
void f() { std::cout << "derived\n"; }
};

int main()
{
base *b1 = new Base;
base *b2 = new Derived;
b1->f(); /* prints "base" */
b2->f(); /* prints "derived" */
delete b1;
delete b2;
return 0;
}

-Mike
Jul 22 '05 #4
Mike Wahler schrieb:
"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41**********************@news.freenet.de...
Mike Wahler schrieb:
"Andreas Lagemann" <an**************@freenet.de> wrote in message
Misunderstanding. 'Base::foo()' is being called because you
supplied a pointer to a 'Base' object. If you want 'Der::foo'
to be called, you need to suppy the address of (or a reference to)
a type 'Der' object.

Sorry but as far as I understand, I supplied a pointer (sorry for the
typo) to Der (i.e. &der) when initializing Bogus. Therefore if I call
bogus->bar() Der::foo should be called, shouldn´t it ?

'bogus' is not a pointer to your base class. It's a pointer
to a type 'Bogus' object.

Yes, but Bogus has a member of type Base*, which is initialized with a
pointer to Der.

Oh I just realized that I made another error defining Bogus::bar().
It should be

A* Bogus::bar()
^^
{
...
return myBase->foo();
^^^^^^
...
}

So any instance of Bogus initialized with a pointer to Der should
execute Der::foo when Bogus::bar() is called. Is that right ?
Perhaps another Question for clarification:
I want to be able to call a specialized versions of a certain function
without knowing the exact type of specialization at compile time. That
is what I thought virtual functions were intended for ...

Can please anyone point out, how I could achieve such a behaviour ?

Assign the address of a derived object to a pointer to an object
of its base class.

#include <iostream>

class Base
{
public:
virtual void f() { std::cout << "base\n"; }
};

class Derived : public Base
{
public:
void f() { std::cout << "derived\n"; }
};

int main()
{
base *b1 = new Base;
base *b2 = new Derived;
b1->f(); /* prints "base" */
b2->f(); /* prints "derived" */
delete b1;
delete b2;
return 0;
}


That´s just how I thought it should work. So my example should work the
same way, doesn´t it ?

Thank you very much for your patience, I´m kinda confused by now ...

Andreas
Jul 22 '05 #5

"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41***********************@news.freenet.de...
Mike Wahler schrieb:
"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41**********************@news.freenet.de...
Mike Wahler schrieb:

"Andreas Lagemann" <an**************@freenet.de> wrote in message
Misunderstanding. 'Base::foo()' is being called because you
supplied a pointer to a 'Base' object. If you want 'Der::foo'
to be called, you need to suppy the address of (or a reference to)
a type 'Der' object.
Sorry but as far as I understand, I supplied a pointer (sorry for the
typo) to Der (i.e. &der) when initializing Bogus. Therefore if I call
bogus->bar() Der::foo should be called, shouldn´t it ?

'bogus' is not a pointer to your base class. It's a pointer
to a type 'Bogus' object.

Yes, but Bogus has a member of type Base*, which is initialized with a
pointer to Der.


That doesn't matter. You were not using that pointer in your
function call.

Oh I just realized that I made another error defining Bogus::bar().
It should be

A* Bogus::bar()
^^
{
...
return myBase->foo();
^^^^^^
...
}

So any instance of Bogus initialized with a pointer to Der should
execute Der::foo when Bogus::bar() is called. Is that right ?
No. The pointer through which you call a virtual function
must be a pointer to the base class. You were not doing that.
Perhaps another Question for clarification:
I want to be able to call a specialized versions of a certain function
without knowing the exact type of specialization at compile time. That
is what I thought virtual functions were intended for ...

Can please anyone point out, how I could achieve such a behaviour ?

Assign the address of a derived object to a pointer to an object
of its base class.

#include <iostream>

class Base
{
public:
virtual void f() { std::cout << "base\n"; }
};

class Derived : public Base
{
public:
void f() { std::cout << "derived\n"; }
};

int main()
{
base *b1 = new Base;
base *b2 = new Derived;
b1->f(); /* prints "base" */
b2->f(); /* prints "derived" */
delete b1;
delete b2;
return 0;
}


That´s just how I thought it should work. So my example should work the
same way, doesn´t it ?


No. Your code does not do the same thing.
Thank you very much for your patience, I´m kinda confused by now ...


I can tell. :-) I suggest you write a much simpler program first,
until you really understand. I think all that other stuff is
only serving to obscure things.
-Mike
Jul 22 '05 #6

"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41***********************@news.freenet.de...
Hi,
after browsing FAQ and archive for a while I decided that the following is
a legal question.

Consider this:

Class Base
{
public:
Base() {}

Base(A* a, B* b);

virtual A* foo(P* p, S& s); // using myA and myB
protected:
A* myA;
B* myB;
};

class Der : public Base
{
public:
Der(O* o) : Base(), myO(o) {}
A* foo(P* p; S& s); // not using myA and myB
private:
O* myO;
};

class Bogus
{
public:
Bogus(Base* b) : myBase(b) {}

void bar()
{
...
myBase->foo(p,s);
...
}
private:
Base* myBase;
};

int main(/*args*/)
{
...
Der der(someO);
Bogus* bogus = new Bogus(der);
...
bogus->bar();
...
};

Executing this results in Base::foo being called instead of Der::foo (what
I would expect) ....
What am I doing wrong ?
Is that because cstr of Der does not initialize any mebers of Base ?
How could I possibly avoid that or get the behaviour I need ?
Pleas help!!!

Regards,

Andreas Lagemann


Looks like things are a bit muddled here? :-)

What you're trying to do *should* work. If you have a class that has a
Base* pointer, and you initialize that pointer with the address of a Der
object, then when that class (Bogus) calls a virtual function using its
Base* pointer, it will resolve to the correct call in the Der class.

So, something is wrong with what you've shown.

As Mike suggests, try a simpler example first to be sure you know what is
happenning. Then you can implement it in a more complex environment.

(One other thing: if you're using VisualC++ 6, upgrade to 7. VC++ 6 is not
very standard-compliant. I don't know in which areas exactly, but it's
*possible* this could be one of them...?)

-Howard

Jul 22 '05 #7

"Andreas Lagemann" <an**************@freenet.de> wrote in message
news:41***********************@news.freenet.de...
Hi,
after browsing FAQ and archive for a while I decided that the following is
a legal question.

Consider this:

Class Base
{
public:
Base() {}

Base(A* a, B* b);

virtual A* foo(P* p, S& s); // using myA and myB
protected:
A* myA;
B* myB;
};

class Der : public Base
{
public:
Der(O* o) : Base(), myO(o) {}
A* foo(P* p; S& s); // not using myA and myB
private:
O* myO;
};

class Bogus
{
public:
Bogus(Base* b) : myBase(b) {}

void bar()
{
...
myBase->foo(p,s);
...
}
private:
Base* myBase;
};

int main(/*args*/)
{
...
Der der(someO);
Bogus* bogus = new Bogus(der);
...
bogus->bar();
...
};

Executing this results in Base::foo being called instead of Der::foo (what
I would expect) ....
What am I doing wrong ?
Is that because cstr of Der does not initialize any mebers of Base ?
How could I possibly avoid that or get the behaviour I need ?
Pleas help!!!

Regards,

Andreas Lagemann


Looks like things are a bit muddled here? :-)

What you're trying to do *should* work. If you have a class that has a
Base* pointer, and you initialize that pointer with the address of a Der
object, then when that class (Bogus) calls a virtual function using its
Base* pointer, it will resolve to the correct call in the Der class.

So, something is wrong with what you've shown.

As Mike suggests, try a simpler example first to be sure you know what is
happenning. Then you can implement it in a more complex environment.

(One other thing: if you're using VisualC++ 6, upgrade to 7. VC++ 6 is not
very standard-compliant. I don't know in which areas exactly, but it's
*possible* this could be one of them...?)

-Howard


Jul 22 '05 #8
"Howard" <al*****@hotmail.com> wrote in message
news:VM*******************@bgtnsc05-news.ops.worldnet.att.net...
(One other thing: if you're using VisualC++ 6, upgrade to 7. VC++ 6 is not very standard-compliant. I don't know in which areas exactly, but it's
*possible* this could be one of them...?)


FWIW:
One of the compilers I use is VC++v6.0 (SP6), and it handles
polymorphism just fine.

-Mike
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
14
by: mshetty | last post by:
Hi, I get an error "Warning: b::a_method hides the virtual function a::a_method()." on compiling the following code.. #include <iostream.h> class a {
3
by: scott | last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do as best i can. i have a parent class that has a vertual function, lets call it virtual int A(). That vertual function...
0
by: tiwy | last post by:
On Andreas Lagemann <andreas.lagemann@freenet.de> wrote: > > Class Base > { > public: > Base() {} > > Base(A* a, B* b); >
12
by: Peter Cranz | last post by:
hello, I've got the following problem: I have a construct similar like this: namespace A { class X {
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
3
by: Klaas Vantournhout | last post by:
Hi, Recently I obtained a problem with virtual inheritance when implementing it in multiple files. To present the problem I have included at the bottom of this post the code of the 4 files. I...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.