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

polymorphic call in abstract class's constructor

Is it possible to have the constructor in an interface call member functions
polymorphically? For instance:
==============================
// INTERFACE CLASS
class SomeInterface {
public:
virtual void foo()=0;
SomeInterface() { foo();}
};

// DERIVED CLASS
class Widget: public SomeInterface {
public:
void foo() { std::cout << "I am the widget" ;}
Widget(): SomeInterface() {};
};

//
int main() {
Widget w;
return 0;
}
================================
well, the compiler complains that there is no implementation for
"bla bla SomeInterface::foo()" (of course there isn't!)...

greetings,
levant.
Jul 22 '05 #1
5 1851
"S. Levent Yilmaz" <sl**@pitt.edu> wrote...
Is it possible to have the constructor in an interface call member functions polymorphically?


No. Any call to a virtual function in a constructor (or destructor)
body is resolved statically, not polymorphically.

Victor
Jul 22 '05 #2

"S. Levent Yilmaz" <sl**@pitt.edu> wrote in message news:bq**********@usenet01.srv.cis.pitt.edu...
Is it possible to have the constructor in an interface call member functions
polymorphically? For instance:


No. You have two issues.

First, it's undefined behavior to make a virtual call to a pure virtual function in the
constructor or destructor.

Second, even if the function wasn't pure (just virtual), the overrider is selected
on the basis of the class for the constructor being run, not the most derived being
constructed. If Someinterface::foo() was just (non-pure) virtual, it's constructor
would call Somerinterface::foo() not WIdget::foo();
Jul 22 '05 #3
Ron Natalie wrote:
No. You have two issues.First, it's undefined behavior to make a virtual call to a pure virtual function in the constructor or destructor.


Indeed, I needed to implement such a behaviour:
- All derived classes carry out certain common operations during
construction
- These common operations are "similar" but not identical, and must be
defined in the particular derived class.

For example (a working version):

class Interface {
virtual void foo1() = 0;
virtual void foo2() = 0;
void foo3() = 0;
};

class Derived1: public Interface {
/* implement all pure virtuals */
Derived1 {
foo1(); foo2() ; foo3();
// do other stuff
}
};

class Derived2: public Interface {
/* implement all pure virtuals */
Derived2 {
foo1(); foo2() ; foo3();
// do other stuff
}
};
/* etc.... */
Well, instead of doing as above, I thought it would be neater to call all
these routine tasks (the foo's) automatically from a single place (Base
class's constructor would be a nice candidate but apparently it is not
working) and thereby guarantee that these routines are carried out for every
derived class.

So is there a neater way?

levent.
Jul 22 '05 #4

"S. Levent Yilmaz" <sl**@pitt.edu> wrote in message news:bq**********@usenet01.srv.cis.pitt.edu...
Ron Natalie wrote:
No. You have two issues.First, it's undefined behavior to make a virtual call to a pure virtual function in the constructor or destructor.


Indeed, I needed to implement such a behaviour:
- All derived classes carry out certain common operations during
construction
- These common operations are "similar" but not identical, and must be
defined in the particular derived class.


If I understand you correctly, you can do the following:

class Interface {
virtual void foo1() = 0;
...
void DoFoos() {
foo1(); foo2(); foo3();
}
};

class Derived1 : public Interface {
Derived1() {
DoFoos();
};
void foo1() { ... }
};

Here you are OK, DoFoos() is called in the context of Derived1's constructor
so the Derived1::foo1() is called. It's defined behavior as long as Derived1
is no longer abstract.
Jul 22 '05 #5

"Ron Natalie" <ro*@sensor.com> skrev i en meddelelse
news:3f*********************@news.newshosting.com. ..

"S. Levent Yilmaz" <sl**@pitt.edu> wrote in message news:bq**********@usenet01.srv.cis.pitt.edu...
Ron Natalie wrote:
> No. You have two issues.First, it's undefined behavior to make a
virtual call to a pure virtual function in the
> constructor or destructor.


Indeed, I needed to implement such a behaviour:
- All derived classes carry out certain common operations during
construction
- These common operations are "similar" but not identical, and must be defined in the particular derived class.


If I understand you correctly, you can do the following:

class Interface {
virtual void foo1() = 0;
...
void DoFoos() {
foo1(); foo2(); foo3();
}
};

class Derived1 : public Interface {
Derived1() {
DoFoos();
};
void foo1() { ... }
};

Here you are OK, DoFoos() is called in the context of Derived1's

constructor so the Derived1::foo1() is called. It's defined behavior as long as Derived1 is no longer abstract.

You can, but the problem here is if you introduce a class that is derived
once again:

class Derived2: public Derived1 { ...}

Here the OP wants foo1, foo2 and foo3 of Derived1 called and this is not
going to happen.
A solution just popping up in my head is to use a template class like this:

template< class doubleconstruct >
class IMPL: public doubleconstruct
{
IMPL(): doubleconstruct() { foo1(); foo2(); foo3();}
};

(With some extra sugar if anything more than the empty constructor is
needed)

Kind regards
Peter Koch Larsen
Jul 22 '05 #6

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

Similar topics

15
by: Tee | last post by:
Hi, I have a base usercontrol with a method (blank method, no code), I have another few usercontrols that will inherit this base usercontrol, but I want to force all the usercontrol that...
1
by: Daniel Suen | last post by:
Hi All, I am trying to implement various kinds of finite automaton simulators; I have abstract the transition mappings to be from a "Set" to another "Set" taking on some "Input". I come up with...
7
by: Mr. Ed | last post by:
I have a base class which has about 150 derived classes. Most of the derived classes are very similar, and many don't change the base class at all. All the derived classes have a unique factory...
3
by: WithPit | last post by:
I am trying to create an managed wrapper but have some problems with it by using abstract classes. In my unmanaged library code i had the following three classes with the following hierarchy ...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
7
by: Rafał Maj Raf256 | last post by:
There was a way to trinck program to call purly abstract non-existing method (causing undefined behaviour), I would like to try that for curiosity (how different compilers/platforms react), how to...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
11
by: Angus | last post by:
I am developing a server which receives a range of different messages. There are about 12 different message types so I thought that a good idea would be to devise a class for each message type....
3
by: Daniel Kraft | last post by:
Hi, I usually program in C++, but for a special project I do have to code in C (because it may be ported to embedded-like platforms where no C++ compiler exists). I do want to realize some...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.