//CCharacter.h/////
class CCharacter{
private:
char m_Name[15];
public:
CCharacter();
~CCharacter();
char *GetName() const;
}
//CCharacter.cpp////
........
char *CCharacter::GetName() const
{
return m_Name;
}
const char [ ] to char * error!
hi man.
try a type cast :
char *CCharacter::GetName()const
{
return (char*)m_Name;
}
or
char *CCharacter::GetName()const
{
return (char*)&(m_Name[0]);
}
try a type cast :
char *CCharacter::GetName()const
{
return (char*)m_Name;
}
or
char *CCharacter::GetName()const
{
return (char*)&(m_Name[0]);
}
No! A type cast for this is bad, bad, bad!
Just change your function signature to:
const char *GetName() const;
[edited by - alexk7 on March 19, 2002 8:57:26 AM]
Just change your function signature to:
const char *GetName() const;
[edited by - alexk7 on March 19, 2002 8:57:26 AM]
Oh~~~I am confuse the signature in C++ now.
Firstly, if i have a function like that:
a.)
char *GetName() const; <-What does it mean?
Then,
b.)
const char *GetName(); <-What does it mean?
Finally,
c.)
const char *GetName() const; <-What does it mean?
Can give me explain their differents? Thank you!
Firstly, if i have a function like that:
a.)
char *GetName() const; <-What does it mean?
Then,
b.)
const char *GetName(); <-What does it mean?
Finally,
c.)
const char *GetName() const; <-What does it mean?
Can give me explain their differents? Thank you!
quote:Original post by wah_on_2
Oh~~~I am confuse the signature in C++ now.
Firstly, if i have a function like that:
a.)
char *GetName() const; <-What does it mean?
Then,
b.)
const char *GetName(); <-What does it mean?
Finally,
c.)
const char *GetName() const; <-What does it mean?
Can give me explain their differents? Thank you!
a) char* GetName() const;
That returns a pointer to a character array and guarantees the function will not modify *this, ie, it could be called on a const CCharacter object.
b) const char* GetName();
That returns a mutable pointer to a constant character array
c) const char* GetName() const;
That returns a mutable pointer to a constant character array and guarantees the function will not modify *this.
quote:Original post by wah_on_2
Oh~~~I am confuse the signature in C++ now.
Firstly, if i have a function like that:
a.)
char *GetName() const; <-What does it mean?
Then,
b.)
const char *GetName(); <-What does it mean?
Finally,
c.)
const char *GetName() const; <-What does it mean?
Can give me explain their differents? Thank you!
a) char* GetName() const;
That returns a pointer to a character array and guarantees the function will not modify *this, ie, it could be called on a const CCharacter object.
b) const char* GetName();
That returns a mutable pointer to a constant character array
c) const char* GetName() const;
That returns a mutable pointer to a constant character array and guarantees the function will not modify *this.
Firstly, mutable variables do not have to be pointers; that is any data member can be mutable.
When a data member is mutable, it can be modified in a ''const'' function (eg. your GetName function).
When a data member is mutable, it can be modified in a ''const'' function (eg. your GetName function).
quote:Original post by Anonymous Poster
Firstly, mutable variables do not have to be pointers; that is any data member can be mutable.
When a data member is mutable, it can be modified in a ''const'' function (eg. your GetName function).
He doesn''t mean mutable, he means mutable (notice, only the first of those is a monospaced keyword).
He means that the pointer can be varied (i.e. it''s mutable), not that the pointer is specially marked as being varied even in const methods (i.e. mutable).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement