Author Topic: do I understand C++ wrong?  (Read 4918 times)

Offline MoonKid

  • Almost regular
  • **
  • Posts: 180
do I understand C++ wrong?
« on: April 22, 2006, 02:05:48 pm »
It is a simple problem.

The compiler (mingw32) says
[error]
main.cpp:27: error: no matching function for call to `ClassB::Function()'
main.cpp:17: note: candidates are: bool ClassB::Function(const char*)
[/error]


Code
#include <iostream>

class ClassA
{
    public:
        bool Function ()
        {
            return true;
        }
};


class ClassB : public ClassA
{
    public:
        bool Function (const char* str)
        {
            return true;
        }
};


int main()
{
ClassB b;

b.Function();

return 0;
}

I do not understand the compilers problem.
Because A::Function and B:Function has different names because the
signaturs (name+parameters) are different.
Shouldn't it work in iso-c++? Is it a compiler problem or I am wrong
understanding standard c++?

PChris

  • Guest
Re: do I understand C++ wrong?
« Reply #1 on: April 22, 2006, 02:15:58 pm »
You have to pass the parameter as well ;)
e.g: Function("Hello");

Offline MoonKid

  • Almost regular
  • **
  • Posts: 180
Re: do I understand C++ wrong?
« Reply #2 on: April 22, 2006, 02:29:06 pm »
You have to pass the parameter as well ;)
e.g: Function("Hello");

No, I do not want to call ClassB::Function. I want to call ClassA::Function. I think the compiler should know that because of the signature.

I know I could call it like that
b.ClassA::Function()

But the compiler should know it by itsefl.

PChris

  • Guest
Re: do I understand C++ wrong?
« Reply #3 on: April 22, 2006, 02:35:49 pm »
I don't really understand you...
If there is a function in ClassB, why should the compiler search the function in ClassA?

Offline MoonKid

  • Almost regular
  • **
  • Posts: 180
Re: do I understand C++ wrong?
« Reply #4 on: April 22, 2006, 02:57:14 pm »
If there is a function in ClassB, why should the compiler search the function in ClassA?

The compiler generates symbols (names) for functions while compiletime.

"bool ClassA::Function()" is a different name instead of "bool ClassB::Function(const char*)".
Not because of A and B. Because of the signature of the methodes.
A methode name is generated on functionname+parameters, that is what is called "signature of a function".

That is what I have learned about compilers.

sethjackson

  • Guest
Re: do I understand C++ wrong?
« Reply #5 on: April 22, 2006, 03:16:45 pm »
The ClassB::Function(const char* str) is different than the ClassA::Function(). You declare ClassB, and then try not passing a param to Fuction(const char* str). You cannot do this since Fuction(const char* str) requires a param. Some C++ guru could answer this better I'm sure, but that is the way the compiler works AFAIK. So why don't you declare ClassA, and call that Function()?

Offline MoonKid

  • Almost regular
  • **
  • Posts: 180
Re: do I understand C++ wrong?
« Reply #6 on: April 22, 2006, 03:20:10 pm »
So why don't you declare ClassA, and call that Function()?

I want to have an object with a to Function() methodes, one with and one without a parameter.
Logicly (in my framework) they should be in different classes.

I will think about how to design it compilable

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: do I understand C++ wrong?
« Reply #7 on: April 22, 2006, 04:02:29 pm »
Code
class ClassB : public ClassA
{
    public:
        using ClassA::Function;
        bool Function (const char* str)
        {
            return true;
        }
};
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: do I understand C++ wrong?
« Reply #8 on: April 22, 2006, 04:06:36 pm »
Your problem is the compiler hides A::Function and it's natural C++ behavior.

You could have lots of different signatures of Function in ClassA. If you make ClassB inherit from ClassA, ClassB will have all those Function too, but if you add a method in ClassB with the name Function (just what you're doing), every inherited signature of Function will be hidden.

To make ClassA::Function visible in ClassB do this (Thomas was faster):

Code: cpp
class ClassB : public ClassA
{
    public:
        using ClassA::Function;

        bool Function (const char* str)
        {
            return true;
        }
};

Another way is to change your main to this:

Code: cpp
int main()
{
ClassB b;

b.ClassA::Function();

return 0;
}