Thread: Making a constructor for a array of character.

  1. #1
    Registered User boneyflesh's Avatar
    Join Date
    Sep 2013
    Posts
    21

    Question Making a constructor for a array of character.

    Code:
    class Robot{
          char name[15]; //assign to char n
          int x, y , direction;
          void ToString(int d);
    public:
          Robot(char *n, int eks, int why, int d);
          void print();
          void Move();
          void Turn(int to);
          bool At(int eks, int why);
          void step();
    };
    
    
    
    
    Robot::Robot(char *n, int eks, int why, int d)//Constructor
    {   
        name=*n;
        eks=x;
        why=y;
        direction=d;
    
    
    }


    Hi guys,, how do i assign char name[] in the class to char n in the constructor?

    pls help,, any explanation or solution will do...

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A few questions to point you in the right direction:
    How will the constructor know how many characters that n points to?
    What sort of functions work according to the answer you just gave?
    How many chars can you copy from n to name?

    And finally, why not simply use std::string and have it just easily work?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User boneyflesh's Avatar
    Join Date
    Sep 2013
    Posts
    21
    thanks for the reply,, i ended up using this constructor
    Code:
    Robot::Robot(char *n, int eks, int why, int d){   
        strcpy(name,n);
        x=eks;
        y=why;
        direction=d;
        
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    That is bad though: strcpy will copy until a null character is found, even if the number of characters copied exceeds that which can be stored in the array, resulting in a buffer overflow.

    If you changed name to be a std::string and then changed the constructor parameter to be a const std::string& you would fix this problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2014, 08:52 AM
  2. Making character to string?
    By jh294 in forum C Programming
    Replies: 8
    Last Post: 04-02-2011, 09:10 AM
  3. How do I remove a character from character array?
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 12-08-2010, 11:27 AM
  4. Making a character MOVE!
    By unknownUser in forum Linux Programming
    Replies: 5
    Last Post: 05-19-2002, 04:42 PM

Tags for this Thread