有没有一种方法可以将一个对象注入到另一个对象的结构中?

时间:2019-07-24 21:16:45

标签: c++ c++11

我试图创建一个驻留在结构中的对象,然后将该结构传递给另一个对象,同时指向相同的地址。这是我要做什么的一个最小示例:

#include <iostream>

class A {
};

struct S {
  A* a;
  S(A* c) { a = c; }
};

class B {
  public:
    B(S* s){ s_ = s; }
    S* get_s() {return s_;}
  private:
    S* s_;
};

int main() {
  A* a = new A();
  S* s = new S(a);
  B b =  B(s);

  std::cout << "a in A = " << &a << "\n";
  std::cout << "a in S = " << &s->a << "\n"; 
  std::cout << "a in B = " << b.get_s() << "\n";

  //output:
  // a in A = 0x7ffe81376918
  // a in S = 0x2563e90
  // a in B = 0x2563e90
}

我希望所有a都指向同一个地址。

2 个答案:

答案 0 :(得分:2)

让我们一步一步地了解您的输出...

std::cout << "a in A = " << &a << "\n";

&a是指向A*的{​​{1}}指针的地址。如果您想要指向的A的地址,只需进行A

std::cout << a

std::cout << "a in S = " << &s->a << "\n"; 为我们提供了指向s->aA的地址的指针。获取该指针的地址将向后退一步。这里A就足够了。

s->a

std::cout << "a in B = " << b.get_s() << "\n"; 将为您提供b.get_s()成员的地址。您可能想要S*

总的来说,您的输出应类似于:

b.get_s()->a

这在ideone上为我提供了以下内容:

  std::cout << "a in A = " << a << "\n";
  std::cout << "a in S = " << &s->a << "\n"; 
  std::cout << "a in B = " << b.get_s()->a << "\n";

答案 1 :(得分:0)

指针是一个变量,其中包含其他一些内存对象的地址。指针(不是诸如unique_ptr之类的类,而是诸如您所使用的从C继承来的指针类型)是一种简单的普通旧数据类型。有关该主题,请参见What are POD types in C++?,以及各种链接,例如What are Aggregates and PODs and how/why are they special?

一旦为某个对象分配了内存并将其分配给指针变量,则地址值可以复制到其他指针变量。这样做意味着所有指针变量(原始变量和副本变量)将具有相同的值,意味着包含相同的地址。

如果您的struct的指针变量作为成员,则只要指针变量成员没有以某种方式修改,它的工作方式相同。一个简单的struct的分配将只进行复制,因此原始指针值将被复制。

这是您发布的资源的修改版本,其中包含一些更正和注释。

#include <iostream>

class A {
};

struct S {
  A* a_s;        // pointer variable for the address of an A
  S(A* c) { a_s = c; }   // constructor takes the value of pointer variable c and assigns it to struct member pointer variable a_s
};

class B {
  public:
    B(S* s){ s_b = s; }
    S* get_s() {return s_b;}
  private:
    S* s_b;
};

int main() {
  A* a = new A();     // construct a new A and assign the address to pointer variable a
  S* s = new S(a);    // construct a new S and provide it the address of the A constructed
  B b =  B(s);        // construct a new B and provide it the address of the S constructed

  // at this point we have the following memory objects.
  //    - an instance of the class A whose address is in a pointer variable "a"
  //    - an instance of the struct S whose address is in a pointer variable s
  //      . the instance's pointer variable "a_s" contains the same value as the local variable "a"
  //    - an instance of the class B which contains a pointer to the struct variable "s"

  // so at this point we have five objects in memory, two constructed with new and three local variables:
  //  - an instance of the class A the address of which is in the local pointer variable "a"
  //  - an instance of the struct S the address of which is in the local pointer variable "s"
  //  - an instance of the class B which is a local variable "b" that is "on the stack"

  // we also have two copies of the address of the class A object which was created:
  //  - the local variable "a"
  //  - the struct S member "a_s" of the object whose address is in local variable "s"

  // we also have two copies of the address of struct S object which was created:
  //  - the local variable "s"
  //  - the class B member "b_s" of the instance in local variable "b"

  // since the class B instance in local variable "b" has a pointer to the struct S whose
  // address is in local variable "s" then the instance doesn't contain a copy of that
  // instance of struct S however it does contain a copy of the address that is contained in the
  // local variable "s".


  std::cout << "a in A = " << a << "\n";         // print the value of the pointer variable "a"
  std::cout << "a in S = " << s->a_s << "\n";      // print the value of the pointer variable "a_s" that is a member of the struct S instance "s"
  std::cout << "a in B = " << b.get_s()->a_s << "\n";  // print value of pointer "a_s" that is a member of struct S instance whose address is in "b.s_b"

  return 0;
}

其输出为:

a in A = 008322D8
a in S = 008322D8
a in B = 008322D8