473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Not virtual good function called

Hi,

This is a code that reproduce my problem.

#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};

class B : public A {
public:
virtual int get(int a) { return 2; }
};

void main()
{
B b;
printf("b->get(12)=%d b->get(1.234)=%d\n", b.get(99), b.get(1.234));
}

The output is:
b->get(12)=2 b->get(1.234)=2
Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??

I think, I have a conversion from float to int then the function
B::get(int) is called. Why ??

Remi Ricard
ri****@gmc.ulaval.ca

Jul 19 '05 #1
4 1699
Remi Ricard wrote:
Hi,

This is a code that reproduce my problem.

#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};
class B : public A {
public:
virtual int get(int a) { return 2; }
This hides any other 'get' function that would have otherwise been
inherited. You can avoid this with

using A::get;
};

void main()
main returns int in C++. The standard requires this. void has never been
an acceptable return type for main.
{
B b;
printf("b->get(12)=%d b->get(1.234)=%d\n", b.get(99), b.get(1.234));
}

The output is:
b->get(12)=2 b->get(1.234)=2
Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??
Because A::get(float) doesn't exist for B. Overload resolution doesn't
cross inheritance boundaries.

I think, I have a conversion from float to int then the function
B::get(int) is called. Why ??


It's the only acceptable overload.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
Remi Ricard escribió:
#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};

class B : public A {
public:
virtual int get(int a) { return 2; }
};

void main()
{
B b;
printf("b->get(12)=%d b->get(1.234)=%d\n", b.get(99), b.get(1.234));
}

The output is:
b->get(12)=2 b->get(1.234)=2

Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??


The get function in B hides all get functions in A. If you want to make
it visible you need to put in B:

using A::get;

Regards.
Jul 19 '05 #3
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
Remi Ricard escribi :
#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};

class B : public A {
public:
virtual int get(int a) { return 2; }
};

void main()
{
B b;
printf("b->get(12)=%d b->get(1.234)=%d\n", b.get(99), b.get(1.2

34));
}

The output is:
b->get(12)=2 b->get(1.234)=2

Why the second call b.get(1.2.3.4) don't call the function A::get(float

) ??

The get function in B hides all get functions in A. If you want to make
it visible you need to put in B:

using A::get;

Regards.


when you call b.get(1.234)), compiler first looks in B. It finds a
function that has the same name and 1.234 can be converted to its ats
argument type (int) so it stops looking further for that name. To call
the A's function, you have to use the method described in the above
postings. Hope that helps.

Regards
Hafiz Abid Qadeer
Jul 19 '05 #4
ha*************@yahoo.com (Hafiz Abid Qadeer) wrote in message news:<9c**************************@posting.google. com>...
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
Remi Ricard escribi :
#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};
class B : public A {
public:
virtual int get(int a) { return 2; }
};
[SNIP] when you call b.get(1.234)), compiler first looks in B. It finds a
function that has the same name and 1.234 can be converted to its ats
argument type (int) so it stops looking further for that name.


The compiler stops even earlier. Even if you couldn't convert
1.234 to int, once the compiler has found one get() it will not
go up one inheritance level. It will search for additional get()
overloads only in the class where it found the first no matter
what type of arguments.

Regards
Jul 19 '05 #5

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

Similar topics

5
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...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
7
by: dc | last post by:
Can anybody think of a situation where virtual function's address resolution/ something related to virtual is done at compile time
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
9
by: ypjofficial | last post by:
Hello All, I am defining a class with one virtual function and storing its first 4 bytes ie. the address of the virtual function table to a file.I am again rereading the file in the same program...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.