Casting!

Hi everybody,
I would like to know if there is a way to cast a char* type
in to a class type!

How I can use s...

char text[80];
cout<< "enter text:";
cin.getline(s,80);

to make a new object with.

class CChess
CChess s; ...??

Thanks a lot



Yeah, as long as you have a constructor for the class that takes a char* type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class CChess
{
public:
    CChess(char* pointer) {classText = pointer;}

private:
    char* classText;
};

//then in main you could
int main()
{
    char text[80];
    cout << "enter text:";
    cin.getline(s, 80);

    CChess s(text); //classText inside the CChess class will have the value of text.
}


I hope that was what you were asking :P

EDIT:
But in this case it's not really called casting. It's just using the constructor of a class to fill out class data.
Last edited on
If you want to take one object and from it create a class you defined yourself, you have to define how it is done. Here is one way; a constructor is made that takes a char* and does something with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

class CChess{
public:
  char* x;
  CChess(char* s);
};

// CChess constructor that accepts a char*
CChess::CChess (char* s)
{
 x = s;
 std::cout << "Constructing CChess object..." << std::endl;
}

int main()
{

// Usage example
char text[80];
 std::cout<< "enter text:";
 std::cin.getline(text,80);
CChess anObject(text);
 std::cout<<anObject.x;
 return 0;
}
How come double (or triple) simultaneous replies happen so often lately? Are the forums overcrowded..

Anyway, if you have a constructor with a (single) char* argument, code like my_object = "hello world"; is perfectly valid. This is typecasting.

edit: "Are the forums overcrowded.." was I supposed to use singular or plural here? I'm confused..
Last edited on
@hamsterman
Lol, wayy to many simultaneous replies.
Plural is correct, because there are multiple forums. Or multiple sections.. Maybe there is only one forum? So then it'd be "Is the forum overcrowded" ... The world will never know.

This is typecasting
I suppose that's right. It's weird thinking of it as typecasting because you made it. But i guess that's how other type casting works.
One forum, many fora. :)
Last edited on
Lol, wayy to many simultaneous replies
It's getting a bit ridiculous.. Maybe I should stop coming here so often.

Maybe there is only one forum?
That's what I want to know. In http://www.cplusplus.com/ there is a link "Forum" which implies that there is only one, but then on http://www.cplusplus.com/forum/ there is a table of forums which would imply that there are many..
@ Moschops

I want the text I've given to be the name of the object itself!


...
// After ...

char text[80];
std::cout<< "enter text:";
std::cin.getline(text,80);

// want to use text to make some new objects with it!

CChess text(...);

Thanks for all of your replies!
I appreciate.
I'm not sure I totally understand what you're saying. Rephrase please?
@ Moschops & Thumper,

It took me a while to unterstand where I went wrong!
What I meant and wanted to do did not make sense !
Well, not much to expect from a rookie.

Your solutions suit my problem much better,
because they're doable.
It's good to know you're there! :)

Thank you very much!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CChess {
  char *name;
  ...

 public:
  CChess(char *n) { name = n; ... }
  ...
};

int main() {
  char text[80];
  std::cout<< "enter text:";
  std::cin.getline(text,80);

  CChess a = text; //or
  CChess b( text ); //or even
  CChess c = (CChess)text

  return 0;
};
@ Mathhead200

Thanks a lot.
BTW, all, constructors should take const char*s.
Topic archived. No new replies allowed.