Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


C++ Template Class -Creating a static method overload in derived class hides the base class method ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 chetanjags   Members   -  Reputation: 203

Like
0Likes
Like

Posted 24 March 2014 - 03:43 AM

Hi I have a some code in my engine similar to following snippet - 

template<class T1,class T2>
class BaseClass
{
public :

static T1*  GetInstance(const T2& initData);

}


struct InitDataType_t
{
}


class DerivedClass : public BaseClass< DerivedClass, InitDataType_t >
{
public:
static T1*  GetInstance(const cstring& somestring);
}


void main()
{
 InitDataType_t initData;

 DerivedClass instance = DerivedClass::GetInstance(initData);
}

// I get a compile time error in vc++ 2013 that cannot convert initData to cstring ??

So basically what I am trying to do here is there's a static method in template base class and I am trying to create an overload with string parameter. I want to use both of them.

But the problem is if I create the static method overload in derived class it hides the base class version. I am not sure if I am missing something here or this is the intended behavior ????

Right now as a work around I am using BaseClass<DerivedClass,InitDataType_t>::GetInstance(initData); inside main.

 

Also If I don't create any overload in derived class then DerivedClass::GetInstance(initData); works fine without any warning or error. Which I think how it should be.

 

Creating a static method overload in derived class hides the base class method, which I feel is a bug or maybe I am missing something here? So can anyone please provide an insight to whats really going on here ?


Edited by chetanjags, 24 March 2014 - 04:57 AM.

My Game Development Blog : chetanjags.wordpress.com


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8423

Like
4Likes
Like

Posted 24 March 2014 - 04:14 AM

Hi I have a some code in my engine similar to following snippet - 

...

So basically what I am trying to do here is there's a static method in template base class and I am trying to create an overload with string parameter. I want to use both of them.

But the problem is if I create the static method overload in derived class it hides the base class version. I am not sure if I am missing something here or this is the intended behavior ????

Right now as a work around I am using BaseClass<DerivedClass,InitDataType_t>::GetInstance(initData); inside main.

 

Also If I don't create any overload in derived class then DerivedClass::GetInstance(initData); works fine without any warning or error. Which I think how it should be.

 

Creating a static method overload in derived class hides the base class method, which I feel is a bug or maybe I am missing something here? So can anyone please provide an insight to whats really going on here ?

The behavior is correct and indended. You're not overloading here, but overriding. Overriding a base class function in a derived class hides the base class definition unless explicitly qualified with the base class name as you discovered. This applies to non-static member functions as well, not just static ones.

 

You can "unhide" the base class symbols in the derived class with the using statement. Something like this should work:

class DerivedClass : public BaseClass< DerivedClass, InitDataType_t >
{
using BaseClass::GetInstance;
public:
static T1* GetInstance(const cstring& somestring);
}

Edited by Brother Bob, 24 March 2014 - 04:14 AM.


#3 chetanjags   Members   -  Reputation: 203

Like
0Likes
Like

Posted 24 March 2014 - 04:39 AM

 

Hi I have a some code in my engine similar to following snippet - 

...

So basically what I am trying to do here is there's a static method in template base class and I am trying to create an overload with string parameter. I want to use both of them.

But the problem is if I create the static method overload in derived class it hides the base class version. I am not sure if I am missing something here or this is the intended behavior ????

Right now as a work around I am using BaseClass<DerivedClass,InitDataType_t>::GetInstance(initData); inside main.

 

Also If I don't create any overload in derived class then DerivedClass::GetInstance(initData); works fine without any warning or error. Which I think how it should be.

 

Creating a static method overload in derived class hides the base class method, which I feel is a bug or maybe I am missing something here? So can anyone please provide an insight to whats really going on here ?

The behavior is correct and indended. You're not overloading here, but overriding. Overriding a base class function in a derived class hides the base class definition unless explicitly qualified with the base class name as you discovered. This applies to non-static member functions as well, not just static ones.

 

You can "unhide" the base class symbols in the derived class with the using statement. Something like this should work:

class DerivedClass : public BaseClass< DerivedClass, InitDataType_t >
{
using BaseClass::GetInstance;
public:
static T1* GetInstance(const cstring& somestring);
}

I understand overriding for non static methods makes complete sense to me. 

But overriding static methods, I didn't knew if that was even possible ? (mind blown)
Can you please provide a little insight into how does it works with regards to  memory mapping, vtable and stuff ? Is it like non static methods ?

 

(This problem reminded me of a quote I read somewhere on the internet - "C++ is too much of a language to learn in a single life time".)
 


My Game Development Blog : chetanjags.wordpress.com


#4 tivolo   Members   -  Reputation: 963

Like
1Likes
Like

Posted 24 March 2014 - 04:49 AM

 

 

Hi I have a some code in my engine similar to following snippet - 

...

So basically what I am trying to do here is there's a static method in template base class and I am trying to create an overload with string parameter. I want to use both of them.

But the problem is if I create the static method overload in derived class it hides the base class version. I am not sure if I am missing something here or this is the intended behavior ????

Right now as a work around I am using BaseClass<DerivedClass,InitDataType_t>::GetInstance(initData); inside main.

 

Also If I don't create any overload in derived class then DerivedClass::GetInstance(initData); works fine without any warning or error. Which I think how it should be.

 

Creating a static method overload in derived class hides the base class method, which I feel is a bug or maybe I am missing something here? So can anyone please provide an insight to whats really going on here ?

The behavior is correct and indended. You're not overloading here, but overriding. Overriding a base class function in a derived class hides the base class definition unless explicitly qualified with the base class name as you discovered. This applies to non-static member functions as well, not just static ones.

 

You can "unhide" the base class symbols in the derived class with the using statement. Something like this should work:

class DerivedClass : public BaseClass< DerivedClass, InitDataType_t >
{
using BaseClass::GetInstance;
public:
static T1* GetInstance(const cstring& somestring);
}

I understand overriding for non static methods makes complete sense to me. 

But overriding static methods, I didn't knew if that was even possible ? (mind blown)
Can you please provide a little insight into how does it works with regards to  memory mapping, vtable and stuff ? Is it like non static methods ?

 

(This problem reminded me of a quote I read somewhere on the internet - "C++ is too much of a language to learn in a single life time".)
 

 

 

I'm pretty sure Brother Bob wanted to say it the other way around.

You are creating a new overload in the derived class, and that will hide all overloads from all base classes, unless you explicitly "pull" them into the derived class via "using". That is intended, and correct.

 

There is no such thing as overriding static methods.



#5 chetanjags   Members   -  Reputation: 203

Like
0Likes
Like

Posted 24 March 2014 - 04:56 AM

@tivolo got it. And this is something nice to have.


My Game Development Blog : chetanjags.wordpress.com





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS