2013-02-09 77 views
3

是否有可能拥有包含对结构体的引用的结构体。这些初始化过程如何?请看下面的简短例子。初始化结构体包含对结构体的引用

感谢

typedef struct { 
    int a; 
}typeInner1; 


typedef struct { 
    int b; 
}typeInner2; 


typedef struct { 
    typeInner1 &one; 
    typeInner2 &two; 
}typeOuter; 

void fun2(typeOuter *p){ 
    p->one.a =2; 
    p->two.b =3; 
} 


void fun(typeInner1 &arg1,typeInner2 &arg2){ 
    typeOuter *ptr = new typeOuter;//<-- how to write initializer 
    fun2(ptr); 
} 


int main(){ 
    typeInner1 arg1; 
    typeInner2 arg2; 
    fun(arg1,arg2); 

    //now arg1.a should be 2 and arg2.a=3 
} 

所有输入好,谢谢。我还必须修改typeOuter的typedef以使其工作。下面的完整工作代码为其他人找到这篇文章。

#include <cstdio> 
typedef struct { 
    int a; 
}typeInner1; 


typedef struct { 
    int b; 
}typeInner2; 


typedef struct typeOuter_t { 
    typeInner1 &one; 
    typeInner2 &two; 
    typeOuter_t(typeInner1 &a1, typeInner2 &a2) : one(a1), two(a2) {} 
}typeOuter; 

void fun2(typeOuter *p){ 
    p->one.a =2; 
    p->two.b =3; 
} 


void fun(typeInner1 &arg1,typeInner2 &arg2){ 
    typeOuter *ptr = new typeOuter(arg1,arg2); 
    fun2(ptr); 
} 


int main(){ 
    typeInner1 arg1; 
    typeInner2 arg2; 
    fun(arg1,arg2); 

    //now arg1.a shoule be 1 and arg2.a=3 
    fprintf(stderr,"arg1=%d arg2=%d\n",arg1.a,arg2.b); 
} 
+0

顺便说一句,'typedef结构{...}富;'不是 “地道”在C++中,简单地写'struct foo {...};'是比较平常的(并且可以直接使用'foo'作为类型名称)。 – Mat 2013-02-09 14:33:45

回答

3

typeOuter一个合适的构造函数:

struct typeOuter 
{ 
    typeInner1 &one; 
    typeInner2 &two; 
    typeOuter(typeInner1 &a1, typeInner2 &a2) : one(a1), two(a2) {} 
}; 



void fun(typeInner1 &arg1, typeInner2 &arg2) { 
    typeOuter *ptr = new typeOuter(arg1, arg2); 
    fun2(ptr); 
} 
2

在C++中,你可以为你的struct创建一个构造。结构基本上是public作为默认访问修饰符的类。

struct Example 
{ 
    // struct fields.. 

    Example(); // initialize struct objects. 
    ~Example(); // perform clean up if necessary. 
}; 
1

你的问题不在于有,但与一般的初始化引用struct S变量。引用不能被默认初始化,不管它们是对struct还是对内置类型的引用。

int& x; // ERROR! Non-initialized reference to int 
C& y; // ERROR! Non-initialized reference to a struct C 
int z; 
C w; 
int& a = z; // OK: Initialized reference to int 
C& b = w; // OK: Initialized reference to struct C 

当你的参考是struct的成员变量,而另一方面(不管它们指的是什么类型的),他们必须尽快你struct构造(就像普通的引用)的约束。默认 - 构建您的struct,然后绑定引用不是一个选项,因为它分两步完成,引用在第一步之后将被初始化。

因此,你必须为你的struct提供构造有初始化您参考:然后

struct typeOuter { 
    typeOuter(typeInner1& o, typeInner2& t) : one(o), two(t) { } 
    typeInner1 &one; 
    typeInner2 &two; 
};  

fun()功能应该是这样的:

void fun(typeInner1 &arg1,typeInner2 &arg2){ 
    typeOuter *ptr = new typeOuter(arg1, arg2); 
    fun2(ptr); 
} 
2

前C++ 11,你将需要一个构造函数为你的结构typeOuter,并初始化初始化列表中的成员引用:

typeOuter(typeInner1& i1, typeInner2& i2) : one(i1), two(i2) {} 

用C++ 11您也可直接用初始化列表的选项(没有定义构造函数自己):

typeOuter *ptr = new typeOuter { arg1, arg2 }; 
1

你可以添加一个构造函数,但你也可以使用集合初始化以及工厂功能。这工作在C所有版本++:

struct A 
{ 
    int& i; 
}; 

A make_a(int& i) 
{ 
    A a = {i}; 
    return a; 
} 

int main() 
{ 
    int i = 0; 
    A* a = new A(make_a(i)); 
} 
0

嗯..

#include <cstdio> 

struct typeInner1 
{ 
    typeInner1(int a = 0) : m_a(a) {} // typeInner1 constructor 
    int m_a; 
}; 

struct typeInner2 
{ 
    typeInner2(int b = 0) : m_b(b) {} // typeInner2 constructor 
    int m_b; 
}; 

struct typeOuter 
{ 
    typeOuter(typeInner1& one, typeInner2& two) : m_one(one), m_two(two) {} // typeOuter constructor 

    typeOuter& set(int a, int b) { m_one.m_a = a; m_two.m_b = b; return *this; } 
    typeOuter& print() { printf("typeInner1 a is %i and typeInner2 b is %i\n", m_one.m_a, m_two.m_b); return *this; } 

    typeInner1& m_one; 
    typeInner2& m_two; 
}; 

typeOuter fun(typeInner1& arg1, typeInner2& arg2) 
{ 
    return typeOuter(arg1, arg2); 
} 

int main() 
{ 
    typeInner1 arg1; 
    typeInner2 arg2; 

    fun(arg1, arg2).print().set(101, 202).print().set(202, 303).print(); 

    return 0; 
} 

输出

typeInner1 a is 0 and typeInner2 b is 0 
typeInner1 a is 101 and typeInner2 b is 202 
typeInner1 a is 202 and typeInner2 b is 303