Public Group

why dose this work?

This topic is 4998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all, I know this is going to sound stupid but why does the following code not work
 template<typename type>
type* foo<type>::get()
{
return (type*)bar;
}


It'll give me an error when I assign that to a pointer. However, if I were to make it constant like this:
 template<typename type>
type* foo<type>::get() const
{
return (type*)bar;
}


It works... I don't mind using that code I was going to change it to something like that anyway. But why does the first method not work!?

Share on other sites
Because you are calling a non-const method on a const instance? [smile]

Share on other sites
Neither will compile since "bar" is undefined. And even then, the "const" in your second case won't compile, since it can only be applied to a class method (which is obviously not the case here, since you posted no classes).

Post more code.

Share on other sites
Ok, never mind, Jingo was right I don't know how missed that. Not my day today [lol]

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 12
• 13
• 10
• 11
• 13
• Forum Statistics

• Total Topics
634096
• Total Posts
3015482
×