Inheritance II
-
Hello, I have been getting these annoying error and warning messages from the compiler that I really don't understand. Consider the following code example:
class objA
{
public:
objA(){};
~objA(){};public:
virtual bool Setup(int){};
bool Setup(float){};
};class objB : public objA
{
public:
objB(){};
~objB(){};public:
virtual bool Setup(int){};
};class C
{
public:
C(){};
~C(){};public:
bool doTest()
{
objB b;b.Setup(100); b.Setup(0.0001); // warning C4244: 'argument' : // conversion from 'const double' to 'int', possible loss of data };
};
So here we get a warning message from the compiler as it doesn't know what function to use -- it is as if it doesn't recognise the base-class' method. I know that somehow this has to do with the
virtual
keyword in use, but I really don't know why this is happening. Now consider yet another code example:class _objA
{
public:
_objA(){};
~_objA(){};public:
bool Setup(int){};
bool Setup(float){};
};class _objB : public _objA
{
public:
_objB(){};
~_objB(){};
};class _C
{
public:
_C(){};
~_C(){};public:
bool doTest()
{
_objB _b;\_b.Setup(100); \_b.Setup(0.0001); // no warnings whatsoever! the compiler correctly casts! };
};
Here we have a class being derived from another one but without any virtual methods -- no errors/warnings! Can someone please explain me what are the compiler/C++/ANSI rules I am missing here? Thank you very much, David
-
Hello, I have been getting these annoying error and warning messages from the compiler that I really don't understand. Consider the following code example:
class objA
{
public:
objA(){};
~objA(){};public:
virtual bool Setup(int){};
bool Setup(float){};
};class objB : public objA
{
public:
objB(){};
~objB(){};public:
virtual bool Setup(int){};
};class C
{
public:
C(){};
~C(){};public:
bool doTest()
{
objB b;b.Setup(100); b.Setup(0.0001); // warning C4244: 'argument' : // conversion from 'const double' to 'int', possible loss of data };
};
So here we get a warning message from the compiler as it doesn't know what function to use -- it is as if it doesn't recognise the base-class' method. I know that somehow this has to do with the
virtual
keyword in use, but I really don't know why this is happening. Now consider yet another code example:class _objA
{
public:
_objA(){};
~_objA(){};public:
bool Setup(int){};
bool Setup(float){};
};class _objB : public _objA
{
public:
_objB(){};
~_objB(){};
};class _C
{
public:
_C(){};
~_C(){};public:
bool doTest()
{
_objB _b;\_b.Setup(100); \_b.Setup(0.0001); // no warnings whatsoever! the compiler correctly casts! };
};
Here we have a class being derived from another one but without any virtual methods -- no errors/warnings! Can someone please explain me what are the compiler/C++/ANSI rules I am missing here? Thank you very much, David
u assume that the call to Setup(float) will be called . Well the compiler is seeing a virtual Setup(int) as the only function . I am not aware of considerations where if u have one function as virtual other overloaded methods are not considered in overload resolution . This might be what is happenning . Need to dig into stroustrup and find .
-
Hello, I have been getting these annoying error and warning messages from the compiler that I really don't understand. Consider the following code example:
class objA
{
public:
objA(){};
~objA(){};public:
virtual bool Setup(int){};
bool Setup(float){};
};class objB : public objA
{
public:
objB(){};
~objB(){};public:
virtual bool Setup(int){};
};class C
{
public:
C(){};
~C(){};public:
bool doTest()
{
objB b;b.Setup(100); b.Setup(0.0001); // warning C4244: 'argument' : // conversion from 'const double' to 'int', possible loss of data };
};
So here we get a warning message from the compiler as it doesn't know what function to use -- it is as if it doesn't recognise the base-class' method. I know that somehow this has to do with the
virtual
keyword in use, but I really don't know why this is happening. Now consider yet another code example:class _objA
{
public:
_objA(){};
~_objA(){};public:
bool Setup(int){};
bool Setup(float){};
};class _objB : public _objA
{
public:
_objB(){};
~_objB(){};
};class _C
{
public:
_C(){};
~_C(){};public:
bool doTest()
{
_objB _b;\_b.Setup(100); \_b.Setup(0.0001); // no warnings whatsoever! the compiler correctly casts! };
};
Here we have a class being derived from another one but without any virtual methods -- no errors/warnings! Can someone please explain me what are the compiler/C++/ANSI rules I am missing here? Thank you very much, David
No,
virtual
has nothing to do with it since you're not calling any methods through a pointer. In your first example,B::Setup()
hides all methods inA
namedSetup()
. At the point of call,B::Setup(int)
is the only method named "Setup" that is visible. The reason you get no warning in the second example is because you removedB::Setup()
, so bothA::Setup()
overloads are visible. --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are Dumb -
No,
virtual
has nothing to do with it since you're not calling any methods through a pointer. In your first example,B::Setup()
hides all methods inA
namedSetup()
. At the point of call,B::Setup(int)
is the only method named "Setup" that is visible. The reason you get no warning in the second example is because you removedB::Setup()
, so bothA::Setup()
overloads are visible. --Mike-- LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ | You Are DumbI think you are right! Your convoluted answer explaned what I dislike aboute C++. Do not get me wrong! C++ is great, it simplifies so many things. But you need to know at leased 10 times as much (than C) in order to acomplish those goals. INTP