Sign in to follow this  
Asuralm

virtual function problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
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;
}
}
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this