开发者

Why Can't I do this in C++? [closed]

开发者 https://www.devze.com 2023-01-23 10:05 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that 开发者_StackOverflow社区it can be reopened, visit the help center. Closed 12 years ago.

Why the method getName() is wrong ?

#include <iostream.h>
#include <string.h>

class People
{
public:
    char* getName();
    void  setName(char* n);

private:
    char* name;
};

void People::setName(char* n)
{
    name = new char(strlen(n));
    strcpy(name,n);
}  

char* People::getName()
{
    return name;   
}


Because Class should be class.

Now with your edit, you don't declare setName in your class definition.

Compilers are picky (like us). Why don't you show us exactly what the compiler is telling you.


A couple of things here. Firstly, as Ignacio pointed out (assuming this was your question in the first place, it's a bit vague), we have private members so that other classes can't arbitrarily screw with them. We expose what we want other classes to use through a public interface.

Secondly, given that this is C++, I highly recommend you use std::string as opposed to char*. Here's your class again:

#include <string>

class Person
{
private:
    std::string m_Name;

public:
    const std::string& getName() const {return(m_Name);};    
}; // eo class Person

What have we done here?

1) We've got a C++ std::string which handles all the business of strings for us. We don't have to worry about memory-allocation, insufficient buffer sizes, etceteras.

2) It's private, nobody can screw with it, so external classes can't go:

Person p;
p.m_Name = "hahaha, I just messed with you";

3) To let users of our class get access to the name, we return a const std::string&. They can use it, see it, but not modify it (unless, of course, they const_cast<> it away, but we've at least showed our intention).

4) The getter is marked as const. This tells the compiler we won't be altering any members of the class during this call.

I hope this has answered whatever your question was. I have no idea at this point.


Because you expose the underlying structure of private data in the object. Make a copy and return that.


You can do this if you fix the Class typo : it should be class .

Provide compilation error log when you ask this type of question, we can't say anything useful but guess if you don't.


Your buffer allocated is one byte smaller than the input string n. I think you forget the space for trailing '\0'.


This is perfectly legal. What error are you encountering?

PS Class must be lowercase c as in class.


From your edit setName() is not defined in your People class definition.

Also,

name = new char(strlen(n));

Is erronous. You should either declare it like this:

name = new char[strlen(n)];

OR

name = new char[255]; //Constant value.

OR

name = malloc(sizeof(char)*strlen*(n));


Use

#include <iostream> 
#include <string> 

Use string instead of char, after all you are including the string header.

using namespace std;
class People  
{  
public:  
    string getName();    
private:  
    string name;    
};

Returning a handle, char pointer, to private data, name , from getName is a bad idea

0

精彩评论

暂无评论...
验证码 换一张
取 消