2014-10-11 5 views
1

"List"라는 다른 클래스의 생성자에서 초기화 목록을 사용하여 "Winery"클래스의 인스턴스를 초기화하려고합니다. 문제는 내가 Winery 생성자에게 복사 할 와이너리를 건네면 정보를 복사하지 못한다는 것입니다.생성자의 C++ 초기화 목록

와이너리 클래스 이쪽 헤더 파일 : 여기

struct Node 
    { 
     Node(const Winery& winery);  
     Winery item;            
     Node *nextByName;    
     Node *nextByRating;    
    }; 

내 목록 클래스의 생성자 : 여기

class Winery 
{ 
public: 

    Winery(const char * const name, const char * const location, const int acres, const int rating); 
    virtual ~Winery(void); 

    const char * const getName() const { return name; } 
    const char * const getLocation() const { return location; } 
    const int getAcres() const { return acres; } 
    const int getRating() const { return rating; } 

private: 
    char *name; 
    char *location; 
    int  acres; 
    int  rating; 
}; 

내 목록 클래스의 헤더 파일의 관련 부분입니다 :

List::Node::Node(const Winery& winery) : 
item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating()), 
nextByName(nullptr), 
nextByRating(nullptr) 
{ 
} 

나는 내가하고 싶은 모든 일을하고있는 것처럼 보입니다. 생성자에 전달하는 와이너리의 데이터 멤버는 private이므로 정보를 가져 오는 함수를 통해 가져 오려고합니다. 그들은 올바른 순서와 모든 것을 갖추고 있습니다. 포인터를 초기화 한 후에는 포인터가 제대로 작동하지만 정보가 없으므로 여기서는 무엇을해야할지 모릅니다. 궁금한 점이 있다면, 이것은 과제를위한 것이며 우리는 초기화 목록을 사용해야합니다. (나는 그것들없이 시도해 봤지만 그렇게 작동하지 않아서 어떻게 해야할지 모르겠습니다.) 어떤 도움을 주시면 감사하겠습니다! 고맙습니다! locationname 문자열도 할당되거나되지 않는 한,

char *newName = new char[sizeof(name) + 1]; 
char *newLocation = new char[sizeof(location) + 1]; 

은 본질적으로 아무것도하지 않고 :

편집 : 그것의 모습이 선에서

Winery::Winery(const char * const name, const char * const location, const int acres, const int rating) : 
acres(acres), 
rating(rating) 
{ 
    char *newName = new char[sizeof(name) + 1]; 
    char *newLocation = new char[sizeof(location) + 1]; 
} 
+0

이것은 무엇이든 의미 할 수 있기 때문입니다. 소멸자는 어떻게 생겼습니까? 내 생각 엔 소멸자가'newName'과'newLocation' 문자열을 삭제하고있는 것 같습니다. 'std :: string' 대신'const char *'를 사용하지 않는 이유가 있습니까? –

+0

생성자가 초기화 목록을 살펴 보았을 때, 메모리에서'item','nextByName','nextByRating'을 봅니다. 'nextByName'과'nextByRating'는'NULL'으로 초기화되었지만'item'의 네 부분 모두 여전히 Visual Studio의 기본 메모리 값을 가지고 있습니다 (0xcdcdcdcd, 0xfeeffeef 등). 그리고 제가'const char *'는'std :: string'을 사용할 수 없기 때문입니다. – user3698112

답변

1

: 여기 내 와이너리 생성자 이것은 아마도 문제의 근원 일 것이다. 그러나 acresrating은 올바르게 구성되어 있어야합니다. 여기

은 (여기 ideone -> http://ideone.com/v98zpq) 제작 한 작업 버전입니다

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

class Winery 
{ 
public: 
    Winery(const char * const name, const char * const location, const int acres, const int rating) : 
     name(strdup(name)), 
     location(strdup(location)), 
     acres(acres), 
     rating(rating) 
    { 
    } 

    virtual ~Winery(void) 
    { 
     free(name); 
     free(location); 
    } 

    const char * const getName() const { return name; } 
    const char * const getLocation() const { return location; } 
    const int getAcres() const { return acres; } 
    const int getRating() const { return rating; } 

private: 
    char *name; 
    char *location; 
    int  acres; 
    int  rating; 
}; 

struct Node 
{ 
    Node(const Winery& winery); 
    Winery item; 
}; 

Node::Node(const Winery& winery) : 
    item(winery.getName(), winery.getLocation(), winery.getAcres(), winery.getRating()) 
{ 
} 

int main() 
{ 
    Winery winery("Mission Hill Winery", "Kelowna, BC, Canada", 646, 4); 

    Node node(winery); 

    printf("%s\n", node.item.getName()); 
    printf("%s\n", node.item.getLocation()); 
    printf("%i\n", node.item.getAcres()); 
    printf("%i\n", node.item.getRating()); 
} 

출력 : 당신 "는 정보를 복사하는 데 실패"가 무슨 뜻인지 정의해야

Mission Hill Winery 
Kelowna, BC, Canada 
646 
4 
+0

매우 도움이됩니다. 감사합니다. – user3698112