0

I have a virtual get method that looks like this in the super class:

virtual double getPortagem() const;    
double via_Ligacao::getPortagem() const
 {
    return 0;
 }

And in their "child":

double getPortagem();
double auto_Estrada::getPortagem()
{
    return portagem;
}

The thing that is bugging me is if I declare the child method as non const the results will be accoding to the values inserted but if I declare as a const method it will return me a memory position. Could you guys explain me this, please?

8
  • I think this will not compile . Commented Dec 8, 2013 at 11:56
  • 5
    The const status of a function is in the signature of the function, and so having a non-const function in the inherited class makes it a completely new function which hides the original const function. Commented Dec 8, 2013 at 11:56
  • It does compile, but why does it give me memory references if I declare it as a const? Commented Dec 8, 2013 at 11:58
  • 1
    Without const, you're not overriding the function, so (depending on how you call it) you might get zero when you don't expect to. I've no idea what you mean by "return a memory position"; neither function can return anything except a double. Commented Dec 8, 2013 at 11:58
  • 1
    This is not a memory reference. A thing stored as double is just a value. It may look as trash for some reason, but still it is a value like 5, 50 or 5e555. Talking here about memory reference will mislead uncareful readers. You see 'thrash' in that value most probably because it was never initialized properly and it contains semi-random data inside. Of course, it is stored in the memory at some place, and of course it has an address, but at C++-level of terminology, it is a value. It does not have any * nor & in its type that would make it an address/pointer/reference. Commented Dec 8, 2013 at 12:09

2 Answers 2

1

function overriding is not being done as you are making a new function in the child classes by declaring non const function which is not getting any match with the function in the superclass.

Sign up to request clarification or add additional context in comments.

Comments

1

In C++11 you can use the keyword override to ensure that an intended override really is an override:

double auto_Estrada::getPortagem() override
{
    return portagem;
}

Then you get a compilation error if it isn't an override, which this non-const function isn't (since it differs in const-ness from the base class method of the same name).

Instead of overriding the base class function this function shadows the base class function, so that if you call o.getPortagem() where o is const and of class auto_Estrada, the compiler won't find the base class const function, and complain.

In C++03 about the best you could do was to statically assert that the same named base class function could be called with the same arguments (it helped but wasn't guaranteed). Note that C++03 didn't have a static_assert. The usual way to do C++03 static asserts was via a typedef of an array, with negative size where you wanted a compile time error.


Regarding terminology,

what you call a "child class" is a derived class (C++ terminology) or subclass (more general computer science terminology, I believe originally from Smalltalk),

what you call "memory position" appears to be arbitrary bits in an uninitialized variable, interpreted as a floating point value, which is called an indeterminate value.

It's formally Undefined Behavior to use an indeterminate value, so anything can happen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.