Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Calling Base function instead of Derived funstion in C++

Posted on 2011-05-10
9
Medium Priority
?
512 Views
Last Modified: 2012-05-11
I have the following type of set up:

class Base
{
    virtual void aFunc(int a, someBaseClass * sBC);
};

class Derived : Base
{
    virtual void aFunc(int a, someDerivedClass *sDC);
};

Some where in code//
Derived aDerivedClass;
someDerivedClass sDC = new someDerivedClass;
aDerivedClass.aFunc(myInt, sDC);

So here is the problem.  aDerivedClass is calling aFunc in Base instead of Derived.  Any ideas of what is causing this and how I can fix it.
0
Comment
Question by:doug5516
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35735468
Assuming that sDC is really a pointer to someDerivedClass, and that both aFunc member functions are public, the code you posted should call the aFunc from the Derived class.

If it's not, then something else is going on. In which case, it's worthwhile to post the actual code you're using (or at least a representative compilable sample).


There is probably some confusion on your side though about varying the types of arguments. Derived::aFunc hides the Base::aFunc member function, it does NOT override or overload Base::aFunc.
So, when calling the aFunc member function from a Derived object, you'll always call Derived::aFunc, never Base::aFunc.
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 35736238
the function in Derived class must have same prototype than the baseclass function. that means you need

class Derived : public Base
{
    virtual void aFunc(int a, someBaseClass *sBC);
};

with that the call would be virtual and calls Derived::aFunc.

Sara

0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35737969
It looks like you are simply dealing with inheritance and function overloading.  
Basically, the derived class inherites all of the base class functions, and you are overloading the aFunc function by providing two different sets of parameters for two functions that happen to have the same name.


To make the point more obvious, say you have the following:
class Base
{
    virtual void aFunc(int a);
};
class Derived : Base
{
    virtual void aFunc(CString str);
};

When the following code exectues, which function gets called:
Derived* p = new Derived;
CString str;
int i;
p->aFunc( str );      //Derived class gets called because parameter matches Derived class definition
p->aFunc(i);           //Base class gets called because parameter matches Derived class definition
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 53

Expert Comment

by:Infinity08
ID: 35738050
@HooKooDooKu : there is no overloading happening. So, the Base::aFunc (in your example with int) will not be called. See my first reply for further details.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35738521
to add to last comment:

the p->aFunc(i); will either fail to compile or will call Derived::aFunc by taking the int as a CString. i mean to remember the CString class has a constructor that takes an int as first argument and has a TCHAR argument as a default argument. if i remember right the statement compiles and calls Derived::aFunc with CString argument.

Sara
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 35740741
@Infinity08:

I forgot about function hiding.  When you declare a function in a derived class, any function in the base class with the same name becomes "hidden".

So @Doug5516, if your desire is to have the base class with one version of the function aFunc(), and your derived class to have two versions of the function aFunc(), you have to either add a 'using' statement (if the compiler will allow it) or redefine the base class function in the derived class.

So you could either do:...
class Base
{
    virtual void aFunc(int a);
};
class Derived : Base
{
    virtual void aFunc(int a){ Base::aFunc(a); }  
    virtual void aFunc(CString str);
};

OR

class Base
{
    virtual void aFunc(int a);
};
class Derived : Base
{
    using Base::aFunc;  //unhides the base class function
    virtual void aFunc(CString str);
};


The following code was written and complied properly in VS2010 as a concole app.  The application compiled properly with either the 'using' statement OR the commentted out line of code above the 'using' statement.  If I don't include either line of code, I got the Error message "'DerivedClass::X2' : cannot convert parameter 1 from 'int' to 'char *'" because the base class function was hidden.
// TestOverload.cpp : main project file.

#include "stdafx.h"

using namespace System;

class BaseClass
{
public:
	virtual void X2(int n){ ; }
};

class DerivedClass : BaseClass
{
public:
	//virtual void X2(int n){ BaseClass::X2(n); }
	using BaseClass::X2;
	virtual void X2(char* pStr){ pStr[0] = 'X'; }
};

int main(array<System::String ^> ^args)
{
	DerivedClass D;
	D.X2(12);
	D.X2("abc");
    return 0;
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35748754
@doug5516 : may I ask why my first post didn't help you understand what the issue was ? How could I have improved it ?

I'm also not sure whether we've actually fully addressed your problem. Because the code you posted in the question would NOT have the behavior you mentioned. So you are clearly using different code. Could you post the actual code, so we can ensure that you get the answer you need ?
0
 

Author Comment

by:doug5516
ID: 35748799
The answer a chose pointed out that the prototypes had to be the same for the Derived function to override the base function.  I went back and checked my code.  I had thought they were the same but sure enough they were different.  You are correct that the code I posted was clearly not the actual code, but the actual code spans across 8 files and I did not want to have to post that much.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35748854
>> The answer a chose pointed out that the prototypes had to be the same for the Derived function to override the base function.

I thought I pointed out the same thing in the last paragraph of my first post. But maybe it wasn't clear enough.

Anyway, as long as your problem is resolved, I'm happy.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question