2015-11-25 153 views
0

我试图将位于_name中的字符数组传递给名为char的引用。通过引用字符数组传递

但是,将数组指针传递给引用时,它只会显示第一个字符而不是整个字符串。

我的问题是你将如何创建一个字符引用数组来将原始指针复制到它然后显示它?如item.cpp中所示,我们将_name指针复制到名称的引用中,然后返回名称,但它只显示字符串的第一个字符。

我只会显示我的代码的相关部分。

Item.cpp:

void Item::name(const char * name){ 

     strncpy(_name, name , 20); 
} 
const char& Item::name() const{ 
     char& name = *_name; 
     return name; 
} 

ItemTester.cpp:

的Main():

int main(){ 
    double res, val = 0.0; 
    fstream F; 
    SItem Empty; 
    SItem A("456", "AItem", 200); 
    SItem B("567", "BItem", 300, false); 
    //cout << A.name() << endl; 
    B.quantity(50); 
    //cout << Empty << endl; 
    cout << A << endl << B << endl << endl; 
    cout << "Enter Item info for A: (Enter 123 for sku)" << endl; 
    cin >> A; 

    cout << "Copying A in C ----" << endl; 
    SItem C = A; 
    cout << C << endl << endl; 
    cout << "Saving A---------" << endl; 
    A.save(F); 
    cout << "Loading B----------" << endl; 
    B.load(F); 
    cout << "A: ----------" << endl; 
    cout << A << endl << endl; 
    cout << "B: ----------" << endl; 
    cout << B << endl << endl; 
    cout << "C=B; op=----------" << endl; 
    C = B; 
    cout << C << endl << endl;; 
    cout << "Operator ==----------" << endl; 
    cout << "op== is " << ((A == "123") && !(A == "234") ? "OK" : "NOT OK") << endl << endl; 
    cout << "op+=: A += 20----------" << endl; 
    A += 20; 
    cout << A << endl << endl; 
    cout << "op-=: A -= 10----------" << endl; 
    A -= 10; 
    cout << A << endl << endl; 
    cout << "op+=double: ----------" << endl; 
    res = val += A; 
    cout << res << "=" << val << endl << endl; 
    return 0; 
} 

ostream的写

virtual std::ostream& write(std::ostream& os, bool linear)const{ 

    return os << sku() << ": " << name() << endl 
     << "Qty: " << quantity() << endl 
     << "Cost (price * tax): " << fixed << setprecision(2) << cost(); 
    } 

让我知道如果我错过了任何重要的细节和il编辑我的文章。

回答

1

char&是对char的引用,因此只是单个字符。参考字符数组将是char*&

例子:

class Test 
{ 
private: 
    static const size_t maxlen = 100; 
    char* _name; 
public: 
    Test() : _name(new char[maxlen+1]) {} 
    ~Test() {delete _name;} 
    void name(const char* s) 
    { 
     if(strlen(s) >= maxlen) 
      throw "too long"; 
     else 
     { 
      memcpy(_name, s, strlen(s) * sizeof(char)); 
      _name[strlen(s)] = '\0'; 
     } 
    } 
    char*& name() 
    { 
     return _name; 
    } 
}; 

int main() 
{ 
    Test obj; 
    obj.name("testname"); 
    cout<<"Name = "<<obj.name()<<endl; 
    obj.name()[0] = '*'; 
    cout<<"After change: Name = "<<obj.name()<<endl; 
    return 0; 
} 

编辑:

我会改变 “吸” 到这样的:如果你想的方法是 “常量”

char*& Item::name() { 
     return _name; 
} 

其实,在这个类的用户不应该改变数组的元素,或者数组的实际地址,那么你不需要返回一个char * &,你可以简单地返回const char *

const char* Item::name() const { 
     return _name; 
} 

据我所见,char*&型的目的是,客户端将能够更改地址的实际地址。

+0

更改getter函数为char *&给我,ü不能返回_name错误。 – petermytt

+0

@petermytt这可能是由于您将此函数设为const函数的事实。请阅读我最近编辑的答案。另外:不要忘记更改头文件(.h)。 – CForPhone

0

正如CForPhone所指出的,char&不是你真正想要的,你的意思可能是char*。但即便如此,使用char*表示字符串是C.在C++中,你应该使用std::string

const string Item::name() const{ 
     string name(_name); 
     return name; 
} 
+0

没有合适的从“std :: string”到“const char”的转换函数存在 – petermytt

+0

应该是const String {Item :: name()const {',而不是'const char&Item :: name()const {'。 –