Class method with the same name?

Started by
8 comments, last by GodFear 12 years ago
Hi folks,

Howdy?!
I need help on a C++ class methods.
Say my class is:


class CMyClass
{
public:
void MyMethod( int a, int b);
}
class CMyNextClass: public CMyClass
{
public:
void MyMethod( int a, int b, int c);
}
void main()
{
CMyNextClass* pMyClass = new CMyNextClass();

pMyclass->MyMethod( 1, 2, 3);
}



I get an compiler error saying MyMethod function does not take 3 arguments.
I am very new to C++ and classes and is still in learning process.
I am trying to google if what i am doing violates C++ rule but i can't find an answer. I appreciate if someone can help.
Thanks!
Advertisement
Class declaration needs ending semicolon!
And it's pMyClass, not pMyclass

class CMyClass
{
public:
void MyMethod( int a, int b);
}

;
class CMyNextClass: public CMyClass
{
public:
void MyMethod( int a, int b, int c);
}

;
void main()
{
CMyNextClass* pMyClass = new CMyNextClass();

pMy

Class->MyMethod( 1, 2, 3);
}

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

That should actually give you a "class does not take two arguments" error when called with two arguments. Three arguments should be fine. If you want it to take two arguments then you would insert a using CMyClass::MyMethod; in the CMyNextClass definition.
ok my bad, i really didn't copy and paste my code. instead i just manually typed it.
The real code has semi colons at the ending as you have shown.

Also pMyClass->MyMethod( 1, 2, 3 ) is what i actually have in my code.

The compiler error i am getting is that it says that "MyMethod" does not take 3 arguments. It seems like it is expecting the MyMethod function declared and defined in the base class CMyClass.
I am not sure if i am violating the rules of function overloading in my code, but i cannot figure it out.

ok my bad, i really didn't copy and paste my code. instead i just manually typed it.
The real code has semi colons at the ending as you have shown.

Also pMyClass->MyMethod( 1, 2, 3 ) is what i actually have in my code.

The compiler error i am getting is that it says that "MyMethod" does not take 3 arguments. It seems like it is expecting the MyMethod function declared and defined in the base class CMyClass.
I am not sure if i am violating the rules of function overloading in my code, but i cannot figure it out.

Then show your real code.
Otherwise, you code is fine and should not cause 3 parameters error.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

That should actually give you a "class does not take two arguments" error when called with two arguments. Three arguments should be fine. If you want it to take two arguments then you would insert a using CMyClass::MyMethod; in the CMyNextClass definition.


You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?

You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?

No, that's not violation.
But the function in your derived class will hide the visibility of the function in the base class.
That's why SiCrane suggested use "using BaseClass:SomeFunction;" in your derived class.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.


[quote name='GodFear' timestamp='1333374764' post='4927489']
You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?

No, that's not violation.
But the function in your derived class will hide the visibility of the function in the base class.
That's why SiCrane suggested use "using BaseClass:SomeFunction;" in your derived class.
[/quote]

Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.


class CBaseClass
{
public:
void MyMethod( int a );
};
class CDerivedClass: public CBaseClass
{
public:
void MyMethod( int a, int b ); // error!
void AnotherMethod( int a );
void AnotherMethod( int a, int b ); // ok!
};

Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.
]

Not exactly.
This link may help you
http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.


[quote name='GodFear' timestamp='1333375822' post='4927500']
Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.
]

Not exactly.
This link may help you
http://stackoverflow...verloads-of-the
[/quote]

Wow! That link just clarified everything!
Thanks for the link, i really appreciate the help.

This topic is closed to new replies.

Advertisement