const char [ ] to char * error!

Started by
7 comments, last by wah_on_2 22 years ago

  
//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
Advertisement
hi man.

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]
alexk7
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!
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.
oic. Thank you for explaination~

one more question
what is mutable pointer?
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).
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).

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement