473,581 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

*auto virtual* feature

Wouldn't it be nice if we could do something like this:

class Funky{

public: auto virtual void doStuff(){
// dostuff
}

};

class MoreFunky: public Funky{

public: void doStuff(){
// do more stuff
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
// do advanced stuff
}

};

main(){

AdvancedFunky f;
f.doStuff();
}
Now, call to *auto virtual* member doStuff() should follow the same
logic as a constructor call in inheritance chain, i. e. when calling
f.doStuff() we got all doStuff() functions executed from bottom up:

Funky::doStuff( )
MoreFunky::doSt uff()
AdvancedFunky:: doStuff()

I think this feature could save us a lot of headache.
Jul 22 '05 #1
18 2202
"nenad" <ne***@medix.co m.hr> wrote in message
news:41******** *************** ***@posting.goo gle.com...
Wouldn't it be nice if we could do something like this:

class Funky{

public: auto virtual void doStuff(){
// dostuff
}

};

class MoreFunky: public Funky{

public: void doStuff(){
// do more stuff
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
// do advanced stuff
}

};

main(){

AdvancedFunky f;
f.doStuff();
}
Now, call to *auto virtual* member doStuff() should follow the same
logic as a constructor call in inheritance chain, i. e. when calling
f.doStuff() we got all doStuff() functions executed from bottom up:

Funky::doStuff( )
MoreFunky::doSt uff()
AdvancedFunky:: doStuff()

I think this feature could save us a lot of headache.


It is unnecessary and would not likely affect the sales of paracetamol
significantly. Furthermore, it is not for the class at the top to pronounce
from on high how another class's virtual functions should operate.

DW

Jul 22 '05 #2
nenad wrote:
Wouldn't it be nice if we could do something like this:

class Funky{

public: auto virtual void doStuff(){
// dostuff
}

};

class MoreFunky: public Funky{

public: void doStuff(){
// do more stuff
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
// do advanced stuff
}

};

main(){

AdvancedFunky f;
f.doStuff();
}
Now, call to *auto virtual* member doStuff() should follow the same
logic as a constructor call in inheritance chain, i. e. when calling
f.doStuff() we got all doStuff() functions executed from bottom up:

Funky::doStuff( )
MoreFunky::doSt uff()
AdvancedFunky:: doStuff()

I think this feature could save us a lot of headache.


What is the problem with writing the following?

class Funky{
public: virtual void doStuff(){
std::cout << "Funky::doStuff ()\n";
}
};

class MoreFunky: public Funky{
public: void doStuff(){
Funky::doStuff( );
std::cout << "MoreFunky::doS tuff()\n";
}

};

class AdvancedFunky: public MoreFunky{
public: void doStuff(){
MoreFunky::doSt uff();
std::cout << "AdvancedFunky: :doStuff()\n";
}
};

Michael Mellor
Jul 22 '05 #3
> I think this feature could save us a lot of headache.

Would this really save that much? I don't see what it saves other than
writing one line in each function, i.e. :

class Funky{

public : doStuff() {
// dostuff
}

} ;

class MoreFunky: public Funky{

public: void doStuff(){
Funky::dostuff( ) ;
// do more stuff
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
MoreFunky::dost uff() ;
// do advanced stuff
}

} ;

This achieves the behavior you want quite easily without a language
extension and without virtual functions. Moreover, it allows you to
control the order of the functions calls. Suppose you want to have
them executed in order :

AdvancedFunky:: doStuff()
MoreFunky::doSt uff()
Funky::doStuff( )

then just change the code to :

class MoreFunky: public Funky{

public: void doStuff(){
// do more stuff
Funky::dostuff( ) ;
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
// do advanced stuff
MoreFunky::dost uff() ;
}

} ;

Or any other mix you prefer. How would this be accomplished with "auto
virtual"? Would be need another keyword such as "reverse auto virtual"
or "retro virtual"?
Jul 22 '05 #4
du****@mit.edu (Keith H Duggar) wrote in message news:<b4******* *************** ***@posting.goo gle.com>...
I think this feature could save us a lot of headache.
Would this really save that much? I don't see what it saves other than
writing one line in each function, i.e. :

class Funky{

public : doStuff() {
// dostuff
}

} ;

class MoreFunky: public Funky{

public: void doStuff(){
Funky::dostuff( ) ;
// do more stuff
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
MoreFunky::dost uff() ;
// do advanced stuff
}

} ;

This achieves the behavior you want quite easily without a language
extension and without virtual functions. Moreover, it allows you to
control the order of the functions calls. Suppose you want to have
them executed in order :

AdvancedFunky:: doStuff()
MoreFunky::doSt uff()
Funky::doStuff( )

then just change the code to :

class MoreFunky: public Funky{

public: void doStuff(){
// do more stuff
Funky::dostuff( ) ;
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
// do advanced stuff
MoreFunky::dost uff() ;
}

} ;

Or any other mix you prefer. How would this be accomplished with "auto
virtual"? Would be need another keyword such as "reverse auto virtual"
or "retro virtual"?

:) Well, maybe the choice of "auto" is a bit stupid ( I just took
first kw that came to my mind). More appropriate would be
"constructorlik e" :)

Would this really save that much? I don't see what it saves other than
writing one line in each function, i.e. :

class Funky{

public : doStuff() {
// dostuff
}

} ;

class MoreFunky: public Funky{

public: void doStuff(){
Funky::dostuff( ) ;
// do more stuff
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
MoreFunky::dost uff() ;
// do advanced stuff
}

} ;

This achieves the behavior you want quite easily without a language
extension and without virtual functions.


Yes. It is nice in this trivial case. But I don't *want* this kind of
behavior. What I want is constructor-like behavior. Consider this:

class Funky{

public: void doStuff(){
// do stuff
}
};

class MoreFunky: virtual public Funky{

public: void doStuff(){
Funky::doStuff( );
// do more stuff
}
};

class AdvancedFunky: virtual public Funky{

public: void doStuff(){
Funky::doStuff( );
// do advanced stuff
}
};

class SuperFunky: public AdvancedFunky, public MoreFunky{

public: void doStuff(){
MoreFunky::doSt uff();
AdvancedFunky:: doStuff();
// do super stuff
}
};

Now it becomes messy because Funky::doStuff( ) is called twice. And it
gets worse as we multiple-inherit further - more and more
Funky::doStuff( )'s are called.
I made a little mistake putting focus on *virtual member functions*.
The issue here is *multiple/virtual inheritance*.
When we multiple inherit, the virtual inheritance mechanism eliminates
duplicate calls to base class constructors. Why not allow this kind of
behavior to ordinary functions (of course if we want to)?
The constructor call logic is pretty straightforward - each
constructor participates in initialisation of it's own piece of final
object. And it happens automatically, the compiler takes care of this.
Is's not hard to imagine the member function could act the same way -
changing it's piece of object down the inheritance chain( skiping the
duplicate base-calls in case of virtual inheritance).
The best solution to this problem ( at least to my humble knowledge )
is something like this:

class Funky{

public: void doStuff(){
doMyStuff();
}
protected: void doMyStuff(){
// do stuff
}

};

class MoreFunky: virtual public Funky{

public: void doStuff(){
Funky::doMyStuf f();
doMyStuff();
}
protected: void doMyStuff(){
// do more stuff
}
};

class AdvancedFunky: virtual public Funky{

public: void doStuff(){
Funky::doMyStuf f();
doMyStuff();
}
protected: void doMyStuff(){
// do advanced stuff
}
};

class SuperFunky: public AdvancedFunky, public MoreFunky{

public: void doStuff(){
Funky::doMyStuf f();
MoreFunky::doMy Stuff();
AdvancedFunky:: doMyStuff();
doMyStuff();
}
protected: void doMyStuff(){
// do super stuff
}
};

This is pretty ugly as we now have to manage parasite doStuff() member
which does nothing except bunch of calls to doMyStuff()'s. The list of
calls gets biger as we inherit more. Imagine what class would look
like if we'd have dozens of members like this. On top of that, we must
have knowlege of whole inheritance tree if we're up to deriving the
new class.
Instead, I would like to have:

class Funky{

public: constuctorlike void doStuff(){
// do stuff
}
};

class MoreFunky: virtual public Funky{

public: void doStuff(){
// do more stuff
};

class AdvancedFunky: virtual public Funky{

public: void doStuff(){
// do advanced stuff
}
};

class SuperFunky: public AdvancedFunky, public MoreFunky{

public: void doStuff(){
// do super stuff
}
};

The similarity with virtual member is that you define behavior of the
function in base class and it continues to behave that way in all
derived classes. That's what I meant by *auto virtual*. If that
function is at the same time virtual, one would declare it like:

auto virtual virtual void doStuff();

//or better:

constructorlike virtual void doStuff();

//or some fancy new keyword

:)
well, I'm probably talking total bs
Jul 22 '05 #5

"nenad" <ne***@medix.co m.hr> wrote in message
news:41******** *************** ***@posting.goo gle.com...
Wouldn't it be nice if we could do something like this:

class Funky{

public: auto virtual void doStuff(){
// dostuff
}

};

class MoreFunky: public Funky{

public: void doStuff(){
// do more stuff
}

};

class AdvancedFunky: public MoreFunky{

public: void doStuff(){
// do advanced stuff
}

};

main(){

AdvancedFunky f;
f.doStuff();
}
Now, call to *auto virtual* member doStuff() should follow the same
logic as a constructor call in inheritance chain, i. e. when calling
f.doStuff() we got all doStuff() functions executed from bottom up:

Funky::doStuff( )
MoreFunky::doSt uff()
AdvancedFunky:: doStuff()

I think this feature could save us a lot of headache.


How would you implement it?

Jonathan
Jul 22 '05 #6
Oh ok, now that you've mentioned virtual inheritance I think I
understand more clearly what you are looking for. I agree that the
working solution you came up with isn't ideal since it does require
knowledge of the entire inheritance tree and lots of function
tracking.

It is an interesting idea you have to give constructor like behavior
to regular functions. I'd love to hear more informed commentary on the
topic than I can offer. What do you think of this runtime solution? (
I've renamed things a bit for simplicity I hope you don't mind the
change. ) It does eliminate the need for knowing the entire
inheritance hierarchy but necessitates an extra state variable in each
instance as well as an auxiliary function. Clearly not as convenient
as support for "constructo r like" behavior.

#include <iostream>

class Base {
bool __print ;
public :
Base ( ) { print ( ) ; }
void reset ( ) { __print = true ; }
void print ( ) {
if ( __print ) {
std::cout << "Base\n" ;
__print = false ;
}
}
} ;

class Derv1A : virtual public Base {
bool __print ;
public :
Derv1A ( ) { print ( ) ; }
void reset ( ) {
Base::reset () ;
__print = true ; }
void print ( ) {
if ( __print ) {
Base::print() ;
std::cout << "Derv1A\n" ;
__print = false ;
}
}
} ;

class Derv1B : virtual public Base {
bool __print ;
public :
Derv1B ( ) { print ( ) ; }
void reset ( ) {
Base::reset () ;
__print = true ; }
void print ( ) {
if ( __print ) {
Base::print() ;
std::cout << "Derv1B\n" ;
__print = false ;
}
}
} ;

class Derv2 : public Derv1A , public Derv1B {
bool __print ;
public :
Derv2 ( ) { print ( ) ; }
void reset ( ) {
Derv1A::reset () ;
Derv1B::reset () ;
__print = true ; }
void print ( ) {
if ( __print ) {
Derv1A::print() ;
Derv1B::print() ;
std::cout << "Derv2\n" ;
__print = false ;
}
}
} ;

int main ( int argc , char * argv[] ) {

std::cout << "constructo r\n" ;
Derv2 derv2 ;
std::cout << "\nfunction \n" ;
derv2.reset() ;
derv2.print() ;

return 0 ;
}
Jul 22 '05 #7
>
How would you implement it?

Jonathan


On the compiler level of course.
I don't see a problem in implementing it.
Jul 22 '05 #8
> What do you think of this runtime solution? (
I've renamed things a bit for simplicity I hope you don't mind the
change. ) It does eliminate the need for knowing the entire
inheritance hierarchy but necessitates an extra state variable in each
instance as well as an auxiliary function. Clearly not as convenient
as support for "constructo r like" behavior.


Yes, but now you have extra calls to Base::reset(). Ok, it's a
reasonable trade-off for not needing to know the whole hierarchy tree.
But this implementation still looks confusing and the whole thing
feels like doing some kind of a language hack. Having constructor-like
members in this case feels kinda right.
Take a look at the case I'm stuck with:

#include <list>

using std::list;

class Base {
int base_data;
list< Base* > children;
Base * parent;

public: virtual ~Base();
};

class Derv1A : virtual public Base {
int deriv1a_data;
};

class Derv1B : virtual public Base {
int deriv1b_data;
};

class Derv2 : public Derv1A , public Derv1B {
int deriv2_data;
};

It's a polymorphic tree structure. I'd really like to see the
implementation of virtual operator=(...) here. The one that duplicates
children objects and handles future derived classes well.
Jul 22 '05 #9
nenad wrote:
What do you think of this runtime solution? (
I've renamed things a bit for simplicity I hope you don't mind the
change. ) It does eliminate the need for knowing the entire
inheritance hierarchy but necessitates an extra state variable in
each instance as well as an auxiliary function. Clearly not as
convenient as support for "constructo r like" behavior.
Yes, but now you have extra calls to Base::reset(). Ok, it's a
reasonable trade-off for not needing to know the whole hierarchy tree.
But this implementation still looks confusing and the whole thing
feels like doing some kind of a language hack. Having constructor-like
members in this case feels kinda right.


Usually, you should split your function up into a non-virtual part that
is done in the base class and a private virtual function in the derived
classes.
Take a look at the case I'm stuck with:

#include <list>

using std::list;

class Base {
int base_data;
list< Base* > children;
Base * parent;

public: virtual ~Base();
};

class Derv1A : virtual public Base {
int deriv1a_data;
};

class Derv1B : virtual public Base {
int deriv1b_data;
};

class Derv2 : public Derv1A , public Derv1B {
int deriv2_data;
};

It's a polymorphic tree structure. I'd really like to see the
implementation of virtual operator=(...) here.
A virtual operator= isn't very useful, since it would need to alter the
class of the object it's called for when used polymorphically . That,
however, isn't possible.
The one that duplicates children objects and handles future derived
classes well.


How would your suggestion help here?

Jul 22 '05 #10

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

Similar topics

7
1803
by: Karsten Hochkirch | last post by:
Hi, I have just encountered a problem when calling a virtual member function in a constructor. Only the memberfunction of the parent class is called instead of the overloaded function. I have attached a small example. Is that bug or feature. If feature: is there an easy way to get the
26
3960
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol ...
4
2241
by: reb | last post by:
I more or less inherited some code that makes liberal use of STL, among other things. Up to now I've sort of considered templates a kind of black art, but I'm coming around to see how they can be useful. I'd really like them if I could figure out this one problem. I'm porting this code from Mac CodeWarrior to XCode which is using GCC 4.0....
32
4493
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
12113
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming...
8
2883
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
9
24150
by: junlia | last post by:
Does anyone how how can we disable the auto-postback feature in asp.net page? I have to use a server side control in the page, but the auto post back feature in it causes problems for me. Please help! Thanks much, Junlia
1
1459
by: glasssd | last post by:
Hi, I was hoping someone might have some ideas on how I can hack around a problem I'm having with the auto-complete feature of the ComboBox. I have a data entry application that uses a pair of ComboBoxes on the main entry screen. These are configured to use the Suggest Append auto-complete feature with the List Items as the data source. ...
1
19799
DebadattaMishra
by: DebadattaMishra | last post by:
Introduction In case of rich applications, you must have observed that a text field behaves like a dynamic combo box. When the user enters some characters in the text field, a popup will come up with a list of values as suggestions to the user. This feature is called auto completion. In case of Eclipse editor, you must have seen while writing...
0
7868
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8304
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8175
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5364
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3805
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1138
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.