# virtual function problem

This topic is 3576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
That's really quick, Thanks!!!

##### Share on other sites
As an interesting aside, how is the compiler going to inline a virtual function call? :)

##### Share on other sites
Quote:
 Original post by HinchAs 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 on other sites
Quote:
 Original post by HinchAs an interesting aside, how is the compiler going to inline a virtual function call? :)

Foo foo;foo.virtual_function();

##### Share on other sites
Quote:
Original post by Evil Steve
Quote:
 Original post by HinchAs 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 on other sites
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.

##### Share on other sites

This topic is 3576 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628682
• Total Posts
2984206

• 13
• 13
• 9
• 10
• 10