🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #113

Re: Base - derived problem.

#113
From: Tony Colston
Date: Mon, 26 Jul 1999 00:00
107 lines
3296 bytes
I think that your problem has something to do with when an object get
constructed. Here is some example code that is similar to yours.


#include <iostream>

class Base1
{
public:
 Base1() {
 doSomething();
 };
 virtual void doSomething()
 {
 cout << "doSomething called in Base1" << endl;
 };
};

class Derived1 : public Base1
{
public:
 Derived1() : Base1()
 {
 };
 void doSomething()
 {
 cout << "doSomething called in Derived1" << endl;
 };
};

void message(char * s)
{
cout << s << endl;
};

int main()
{
Base1 *x1 = new Derived1();
x1->doSomething();
}

When you run this code you will get two messages from the two different
versions of doSomething(). I do not believe that the virtual table is setup
for the Derived1 object when you are trying to call the doSomething method.
That means that during construction of a Derived1 object the Base1 object is
constructed first and the doSomething() call does not have any other
information at that point ... so that is the method that is called. Once the
Derived1 object is created then the virtual call to doSomething() will work
as you think it should.

I am not sure if there is a way around this kind of stuff. I would guess
that someone else with more C++ might have some better answers.

Tony



Dobieslaw Wroblewski <wrobldob@kki.net.pl> wrote in message
news:wQUm3.8699$D4.125836@news.tpnet.pl...
> I've got something like that in my program:
>
> ------------------------------------------------------------
> class Base {
>   protected:
>     virtual void SetupIt(void) {
>       message("base setup");
>     }
>   public:
>     Base (void) {
>       SetupIt();
>     }
> }
>
> class Derived : public Base {
>   protected:
>     virtual void SetupIt(void) {
>       message("derived setup");
>     }
>   public:
>     Derived (void) : Base () {
>       // additional instructions
>     }
> }
>
>
> Base *x;
>
> x = new Derived();
> -------------------------------------------------------------
>
> And I get the "base setup" message while I want to
> get the "derived setup".
>
> I hope it is possible to do this (using the combination
> of 'virtual' modifiers) without explicitly copying and pasting
> the constructor's contents (which, in fact, is much longer
> in my program;-).
>
> Could anyone help me?
>
>
> --
>
> Yours D. Wroblewski
> ( wrobldob@kki.net.pl , wrobldob@wars.ipipan.waw.pl )
>
>
Technical Details
Message-ID: <7ni1cd$c8s$1@news.socomm.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <wQUm3.8699$D4.125836@news.tpnet.pl>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 108b44,4a24208a4bcf639b
X-Google-Attributes: gid108b44,public
X-Google-Thread: 10d4ba,4a24208a4bcf639b
X-Google-Attributes: gid10d4ba,public
X-Google-Thread: 114592,4a24208a4bcf639b
X-Google-Attributes: gid114592,public
X-Google-Thread: fe105,4a24208a4bcf639b
X-Google-Attributes: gidfe105,public
From: "Tony Colston" <agcolston@buckman.com>
Subject: Re: Base - derived problem.
Date: 1999/07/26
Message-ID: <7ni1cd$c8s$1@news.socomm.net>
X-Deja-AN: 505420695
References: <wQUm3.8699$D4.125836@news.tpnet.pl>
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
Organization: iXL Memphis - http://www.ixlmemphis.net
X-MSMail-Priority: Normal
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.sys.lang.c++,pl.comp.programming