Archived

This topic is now archived and is closed to further replies.

wah_on_2

const char [ ] to char * error!

Recommended Posts


  
//CCharacter.h/////

class CCharacter{
private:
      char m_Name[15];

public:
      CCharacter();
      ~CCharacter();
      char *GetName() const;
}


//CCharacter.cpp////

........

char *CCharacter::GetName() const
{   
      return m_Name;
}
  
complier error:cannot convert const char [15] to char * why? How can i fix it? Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hi man.

try a type cast :

char *CCharacter::GetName()const
{
return (char*)m_Name;
}

or

char *CCharacter::GetName()const
{
return (char*)&(m_Name[0]);
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest 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).

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites