C++ 初始化类构造函数中的结构

C++ 初始化类构造函数中的结构,c++,constructor,struct,C++,Constructor,Struct,如何在类的构造函数中初始化结构指针。 例如: struct my_struct{ int i; char* name; }; class my_class{ my_struct* s1; my_class() { // here i want to make s1->i = 10; and s1->name = "anyname" ; // should i assign it like s1->i= 1

如何在类的构造函数中初始化结构指针。 例如:

struct my_struct{
    int i; 
    char* name;
}; 
class my_class{ 
    my_struct* s1;
    my_class() {
        // here i want to make s1->i = 10; and s1->name = "anyname" ;  
        // should i assign it like s1->i= 10; and call new for s1->name and strcpy(s1->name "anyname");  
        // it compiles in g++ without any warning/error but gives seg fault at run time  
    }
};

因为这是C++,使用<代码> STD::String < /C> >代替<代码> char */COD>:

struct my_struct{
    int i; 
    std::string name;
}; 
class my_class{ 
    my_struct* s1;
    my_class() {
        s1 = new my_struct;
        s1->i = 10;
        s1->name = "anyname";
    }
};
原始代码出现故障的原因是您未能为
s1
分配内存,也未能为
s1->name
分配内存。我用
new
修复了前者,用
std::string
修复了后者。如果由于某种原因无法使用
std::string
,请在尝试使用
strcpy
的位置使用
strdup


最后,别忘了为
my_class
提供一个析构函数,它将删除
s1
(如果您选择
char*
strdup
),它将释放
s1->name

当您创建
my_class
的实例时,
s1
指针不会指向任何东西。您必须为其分配内存,如下所示:

myclass() {
    s1 = new my_struct;
    // initialize variables
}
您还必须为其创建析构函数:

~myclass() {
    // delete variables
    delete s1;
}

也,因为这是C++,所以我建议您使用<代码> STD::String ,而不是<代码> char */COD> S.< /P> < P>我确信您可以使用初始化列表,并且直接使用NeX+init结构。此外,您不能忘记,完成后必须删除指针:

 my_class() {
     s1 = new (my_struct);
     s1->i = 10;
     s1->name = (char *) malloc(strlen("anyname"));
     s1->name = "anyname";
     // here i want to make s1->i = 10; and s1->name = "anyname" ;  
     // should i assign it like s1->i= 10; and call new for s1->name and strcpy(s1->name "anyname");  
     // it compiles in g++ without any warning/error but gives seg fault at run time  
  }


 ~my_class(){
     free(s1->name);
     delete s1;
  }
struct my_struct{
    int i; 
    char* name;
}; 
class my_class{ 
    my_struct* s1;
    my_class() : s1(new my_struct) {
        s1->i = 2;
        s1->name = "Something";
    }
    ~my_class() { delete s1; }
};

另外,请确保您使用的是
char*
,这是有原因的,否则
std::string
通常会更好。

如果结构在类中,您可以使用结构构造函数:

struct my_struct
{
  int i; 
  std::string name;

  my_struct()
  {
    i = 10;
    name = "anyname";
  };
};
如果是全局对象,则首先需要创建对象,然后对其进行初始化:

class my_class
{ 
  my_struct * s1;
  my_class() : s1(new my_struct())
  {
    s1->i = 10;
    s1->name = "anyname";
  }
};

我很惊讶没有人提出以下建议

struct my_struct
{
  int i; 
  std::string name;

  my_struct(int argI, std::string const& argName) : i(argI), name(argName) {}
};

class my_class
{
  my_struct s1;  // no need for pointers!

  my_class() : s1(1, std::string("test name")) {} // construct s1 using the two argument constructor, can also default construct as well.
};

使用这种方法,您不必担心清理
s1
,它是自动的…

记住删除析构函数中的
s1
指针(如果它必须是原始指针)。谢谢aix…我得到了它:)。。但是,我仍然不相信为什么不在C++中使用char *来建议<代码> STD::String < /C>。但是为什么停在这里?我还将
my_-struct*
更改为
std::unique_-ptr
。或者更好的是,将
s1
的定义更改为
std::auto_-ptr s1
std::unique_-ptr s1
。无需在析构函数中删除它。@Vishal:好的,但并非所有编译器都提供对C++11的访问。对于这些编译器,
std::auto_ptr
仍然是一个有效的解决方案,如果有限的话。至少您考虑了
name
的存储-这里的一些其他答案没有…代码是错误的。如果
s->name
类型为
char*
,则使用行
s1->name=“anyname”分配其值
是一种错误的方法,在那之后,我怀疑执行是否会在
free(s1->name)行继续进行而不会崩溃。正确的方法是使用strpcy或它的一个变体+1
我很惊讶没有人提出以下建议…
事实上。。。我震惊于我在这些答案中看到了多少马洛斯/新闻-/