virtual function problem

Started by
6 comments, last by swiftcoder 16 years, 1 month ago
hi all: I have a problem about the virtual function in c++.


class base_class{
public:
...
   inline virtual int somefunction(){ return 3; };
}

class child_class : public base_class{
public:
...
   inline int somefunction() { 
       if (something is true){
           return 3;
       } else {
           return 5;
       }
   }
}

As shown in the above example, when "something is true", I would like to call the somefunction in the base_class. How can I do that? I know in java this can be down by call (super) somefunction(). But how should I do it in C++ please? Thanks
Asura
Advertisement
base_class::somefunction().

E.g.:
class base_class{public:...   inline virtual int somefunction(){ return 3; };}class child_class : public base_class{public:...   inline int somefunction() {        if (something is true){           return base_class::somefunction();       } else {           return 5;       }   }}
That's really quick, Thanks!!!
Asura
As an interesting aside, how is the compiler going to inline a virtual function call? :)
Quote:Original post by Hinch
As an interesting aside, how is the compiler going to inline a virtual function call? :)
I suppose technically, if the compiler is able to track all the way back to the construction of the object - so it knows its type at compile time, it could do it.

Whether it does or not, I don't know [smile]

Also, functions delcared in the class declaration like that are implicitly inline, so the keyword should have no effect.
Quote:Original post by Hinch
As an interesting aside, how is the compiler going to inline a virtual function call? :)


Foo foo;foo.virtual_function();
Quote:Original post by Evil Steve
Quote:Original post by Hinch
As an interesting aside, how is the compiler going to inline a virtual function call? :)
I suppose technically, if the compiler is able to track all the way back to the construction of the object - so it knows its type at compile time, it could do it.

Whether it does or not, I don't know [smile]

Also, functions declared in the class declaration like that are implicitly inline, so the keyword should have no effect.


VC will inline some very trivial virtual functions. It will however do wonders with obvious function pointers.

GCC absolutely refuses to inline any kind of function pointer (by design). I cannot say about virtuals, but I believe it refuses to inline there as well. At least I haven't encountered any such case with 3.4.

I don't really remember this case, but I believe compilers can only reliably inline functions called in the same class, there everything is known in advance - this is why calling this->virtual_function in initializer list doesn't work properly.
Quote:Original post by Antheus
this is why calling this->virtual_function in initializer list doesn't work properly.


Nope, that doesn't work because the subclass' hasn't been constructed until after the superclass' constructor.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement