473,748 Members | 4,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual functions in inherited class

Hi
I have run into an eerie situation whihc i cant make out. I have a
class A which has has two virtual functions with the same name, having
diff arguments. Basically a case of function overloading.

Also there is a class B, which is derived form A and has one of the
functions in the base class overridden.

So this is the scenario.

Class A
{
public:
int virtual test(int a, int b)
{
cout<<"inside A::test(int,int )";
return a+b;
}

float virtual test(float a, float b)
{
cout<<"inside A::test(float,f loat)";
return a+b;

}
}

Class B : public A
{
public:
int test(int a, int b)
{
cout<<"inside B::test(int,int )";
return a + b;
}
}

Now i have the following code in the main function
B* obj = new B();
obj->test(2,3); //this wrks fine
obj->test(2.3,2.5 ); //this is giving me compilation error.
// I dont known why ???

the second call fails during compilation. Ideally it should make a call
to A::test(float, float)

Plz let me know if i m doing anything wrong

Tx
Vaibhav

Dec 15 '06 #1
7 1719

vabby napsal:
Hi
I have run into an eerie situation whihc i cant make out. I have a
class A which has has two virtual functions with the same name, having
diff arguments. Basically a case of function overloading.

Also there is a class B, which is derived form A and has one of the
functions in the base class overridden.

So this is the scenario.

Class A
{
public:
int virtual test(int a, int b)
{
cout<<"inside A::test(int,int )";
return a+b;
}

float virtual test(float a, float b)
{
cout<<"inside A::test(float,f loat)";
return a+b;

}
}

Class B : public A
{
public:
int test(int a, int b)
{
cout<<"inside B::test(int,int )";
return a + b;
}
}

Now i have the following code in the main function
B* obj = new B();
obj->test(2,3); //this wrks fine
obj->test(2.3,2.5 ); //this is giving me compilation error.
// I dont known why ???
2.3 is double, not float. Compiler does not know, whether to convert it
to int or float. Try

obj->test(2.3f, 2.5f);
>
the second call fails during compilation. Ideally it should make a call
to A::test(float, float)

Plz let me know if i m doing anything wrong

Tx
Vaibhav
Dec 15 '06 #2
vabby wrote:
Hi
I have run into an eerie situation whihc i cant make out. I have a
class A which has has two virtual functions with the same name, having
diff arguments. Basically a case of function overloading.

Also there is a class B, which is derived form A and has one of the
functions in the base class overridden.
....and the other one _hidden_. Learn about name hiding, read the FAQ
about it.
>
So this is the scenario.

Class A
C++ is case sensitive. You posted code does not compile. Please next
time do not type your code directly into your posting and instead copy
and paste it from your text editor. Read FAQ 5.8.
{
public:
int virtual test(int a, int b)
{
cout<<"inside A::test(int,int )";
return a+b;
}

float virtual test(float a, float b)
{
cout<<"inside A::test(float,f loat)";
return a+b;

}
}

Class B : public A
{
public:
int test(int a, int b)
{
cout<<"inside B::test(int,int )";
return a + b;
}
}

Now i have the following code in the main function
B* obj = new B();
obj->test(2,3); //this wrks fine
obj->test(2.3,2.5 ); //this is giving me compilation error.
// I dont known why ???
Because 'test(float,flo at)' does not exist in 'B' scope. You need
to bring it in with a "using" declaration. See FAQ.
>
the second call fails during compilation. Ideally it should make a
call to A::test(float, float)

Plz let me know if i m doing anything wrong
Your understanding of name lookup is incomplete, perhaps.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 15 '06 #3
Actuall I just used floats and ints to illustrate the case. I am using
my own classes, instead of ints and floats.

Hence the declarations are as follows:

virtual class1 A::test(class1, class1);

virtual class2 A::test(class2, class2);

virtual class2 B::test(class2, class2);

Dec 15 '06 #4

vabby wrote:
Hi
I have run into an eerie situation whihc i cant make out. I have a
class A which has has two virtual functions with the same name, having
diff arguments. Basically a case of function overloading.

Also there is a class B, which is derived form A and has one of the
functions in the base class overridden.

So this is the scenario.

Class A
{
public:
int virtual test(int a, int b)
{
cout<<"inside A::test(int,int )";
return a+b;
}

float virtual test(float a, float b)
{
cout<<"inside A::test(float,f loat)";
return a+b;

}
}
;

class, not Class
>
Class B : public A
{
public:
int test(int a, int b)
{
cout<<"inside B::test(int,int )";
return a + b;
}
}

Now i have the following code in the main function
B* obj = new B();
obj->test(2,3); //this wrks fine
obj->test(2.3,2.5 ); //this is giving me compilation error.
// I dont known why ???

the second call fails during compilation. Ideally it should make a call
to A::test(float, float)

Plz let me know if i m doing anything wrong
Nothing is wrong. Change the name of test(int,int) to something else
than test and the function will no longer be hidden by the same name in
class B. The same happens if you only had a non-virtual function
test(int, float, float) in class B.

Change test(float, float) to test(char,char) and modify class B to:

class B : public A
{
public:
int test(int a, int b)
{
std::cout<<"ins ide B::test(int,int )\n";
return a + b;

}
using A::test; // !!!
};

int main()
{
B b;
b.test(0 , 1);
b.test('a', 'b'); // and it works
}

Dec 15 '06 #5

"Ondra Holub" <on*********@po st.czwrote in message
news:11******** **************@ 73g2000cwn.goog legroups.com...
>
vabby napsal:
>Hi
I have run into an eerie situation whihc i cant make out. I have a
class A which has has two virtual functions with the same name, having
diff arguments. Basically a case of function overloading.

Also there is a class B, which is derived form A and has one of the
functions in the base class overridden.

So this is the scenario.

Class A
{
public:
int virtual test(int a, int b)
{
cout<<"insid e A::test(int,int )";
return a+b;
}

float virtual test(float a, float b)
{
cout<<"inside A::test(float,f loat)";
return a+b;

}
}

Class B : public A
{
public:
int test(int a, int b)
{
cout<<"inside B::test(int,int )";
return a + b;
}
}

Now i have the following code in the main function
B* obj = new B();
obj->test(2,3); //this wrks fine
obj->test(2.3,2.5 ); //this is giving me compilation error.
// I dont known why ???

2.3 is double, not float. Compiler does not know, whether to convert it
to int or float. Try

obj->test(2.3f, 2.5f);
Sorry, but where did you get this idea from? The reason that for the problem
is that name hiding occurs and like Victor or others pointed out a using
statement needs to be added to bring test(float, float) into the context of
B.
Regarding the conversion there is a clear order of conversions defined by
the standard. Naturally, before a "change of type" - in this case I mean
from floating point to integer - will be considered, the compiler will try
to find a floating point conversion. It is actually capable of doing this
even if the types are of a different size like double and float. In the case
that the value can be represented by the new type no change occurs,
otherwise rounding and a loss of precision will take place and the compiler
might give you a warning.
Cheers
Chris
Dec 15 '06 #6
Chris Theis napsal:
"Ondra Holub" <on*********@po st.czwrote in message
news:11******** **************@ 73g2000cwn.goog legroups.com...

vabby napsal:
Hi
I have run into an eerie situation whihc i cant make out. I have a
class A which has has two virtual functions with the same name, having
diff arguments. Basically a case of function overloading.

Also there is a class B, which is derived form A and has one of the
functions in the base class overridden.

So this is the scenario.

Class A
{
public:
int virtual test(int a, int b)
{
cout<<"inside A::test(int,int )";
return a+b;
}

float virtual test(float a, float b)
{
cout<<"inside A::test(float,f loat)";
return a+b;

}
}

Class B : public A
{
public:
int test(int a, int b)
{
cout<<"inside B::test(int,int )";
return a + b;
}
}

Now i have the following code in the main function
B* obj = new B();
obj->test(2,3); //this wrks fine
obj->test(2.3,2.5 ); //this is giving me compilation error.
// I dont known why ???
2.3 is double, not float. Compiler does not know, whether to convert it
to int or float. Try

obj->test(2.3f, 2.5f);

Sorry, but where did you get this idea from? The reason that for the problem
is that name hiding occurs and like Victor or others pointed out a using
statement needs to be added to bring test(float, float) into the context of
B.
Regarding the conversion there is a clear order of conversions defined by
the standard. Naturally, before a "change of type" - in this case I mean
from floating point to integer - will be considered, the compiler will try
to find a floating point conversion. It is actually capable of doing this
even if the types are of a different size like double and float. In the case
that the value can be represented by the new type no change occurs,
otherwise rounding and a loss of precision will take place and the compiler
might give you a warning.
Cheers
Chris
Hi Chris.

I was wrong when I tried given example, because I have created pointer
to instance of B, but I have assigned it to type of A*:

A* obj = new B(); // In given example was B* obj = ...
obj->test(2,3);
obj->test(2.3,2.5 ); // ###

But anyway. The line marked ### is ambiguous. At least for gcc 4.1.0 on
linux, gcc 3.4.4 on windows, MS Visual Studio .NET 2003, MS Visual
Studio 2005 and Borland C++ 5.5. All of these compilers complain
something like:
main.cpp:38: error: call of overloaded `test(double, double)' is
ambiguous
main.cpp:9: note: candidates are: virtual int A::test(int, int)
main.cpp:15: note: virtual float A::test(float, float)

And I think they are right, because conversion from double to float has
the same priority as conversion from double to int. Compiler does not
care, where is bigger lost of precision.

Correct version of first question example is

#include <iostream>

using namespace std;

class A
{
public:
int virtual test(int a, int b)
{
cout<<"inside A::test(int,int )\n";
return a+b;
}

float virtual test(float a, float b)
{
cout<<"inside A::test(float,f loat)\n";
return a+b;

}

};

class B : public A
{
public:
using A::test;

int test(int a, int b)
{
cout<<"inside B::test(int,int )";
return a + b;

}
};

int main()
{
B* obj = new B(); // In given example was B* obj = ...
obj->test(2,3); //this wrks fine
obj->test(2.3f, 2.5f);
}

Best Regards,
Ondra

Dec 15 '06 #7

"Ondra Holub" <on*********@po st.czwrote in message
news:11******** **************@ f1g2000cwa.goog legroups.com...
[SNIP]
A* obj = new B(); // In given example was B* obj = ...
obj->test(2,3);
obj->test(2.3,2.5 ); // ###

But anyway. The line marked ### is ambiguous. At least for gcc 4.1.0 on
linux, gcc 3.4.4 on windows, MS Visual Studio .NET 2003, MS Visual
Studio 2005 and Borland C++ 5.5. All of these compilers complain
something like:
main.cpp:38: error: call of overloaded `test(double, double)' is
ambiguous
main.cpp:9: note: candidates are: virtual int A::test(int, int)
main.cpp:15: note: virtual float A::test(float, float)

And I think they are right, because conversion from double to float has
the same priority as conversion from double to int. Compiler does not
care, where is bigger lost of precision.
You're of course right as there is no promotion but rather a "real"
conversion involved. I'm sorry that I didn't think of that while writing my
previous post.

Cheers
Chris
Dec 16 '06 #8

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

Similar topics

4
4426
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with one vtbl ptr BUt when I derive the class B from class A (both have 1 virtual function each ) the size remains still as 4 bytes . class A = 4 bytes class B :public A = 4 bytes class...
5
2926
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new virtual function plus redefinitions of the three inherited virtual functions. Following is a simplified code fragment to illustrate my code and my problem:
19
2343
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
9
1907
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
5
1620
by: gouqizi.lvcha | last post by:
Hi, all: I have 3 class X, Y, Z class Y is a subclass of class X; class Z is a subclass of class Y; i.e. class Y : public class X class Z : public class Y
6
3137
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm little confused. Am I deleting the right object when I call the virtual deconstructor? I was under impression when creating a class if I'm not specifically allocating memory, do not implement deconstructor and let the default take care of it...
3
4551
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
12
2661
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read up on virtual inheritance and from my understanding this should work fine but I haven't found any docs that use such a tangled example. The gcc output containing the errrors: Example.cpp: In member function 'virtual IObject*...
23
4612
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
17
3543
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
8991
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9247
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.