[英]parameterized constructor with dynamic array of object
In the below program i have used two classes, and i am trying to relate the with aggregation, i have declare class A as private in class B, and with the help of constructor i am initizing base address of class A object to private member of class B object 即(一个对象)。 我正在尝试使用参数化构造函数将 class A 值传递给 class B ,但我得到的垃圾值如下:
#include <iostream>
using namespace std;
void create (B * &obj2, int siz)
{
std::cout << obj2[0].get_nn (); //this will run fine
for (int i = 0; i < siz; i++)
obj2[i] = B (10, "pranjal"); //this will also run fine
std::cout << obj2[0].get_nn ();
}
// same line printing again, this will not give output
// ***************************************
void display ()
{
std::cout << object.get_data () << object.get_stringdata ();
}
// giving garbage values
// why is it happening
// *********************************program
// ************************************** enter code here
// Online C++ compiler to run C++ program online
class A {
int rool;
string name;
public:
A () { };
A (int a, string name);
int get_data () {
return rool;
}
string get_stringdata () {
return this->name;
}
};
A::A (int a, string name)
{
this->rool = a;
this->name = name;
}
void getdetails (A * obj)
{
for (int i = 0; i < 3; i++)
obj[i] = A (20, "pranjal");
}
class B {
int bbb;
string name;
A object;
public:
B () {};
B (A s) {
object = s;
}
string get_nn () {
return object.get_stringdata ();
}
B (int a, string b);
void display () {
std::cout << object.get_data () << object.get_stringdata ();
}
};
void dis (B * obj2)
{
for (int i = 0; i < 2; i++) {
obj2[i].display ();
}
}
void create (B * &obj2, int siz)
{
std::cout << obj2[0].get_nn ();
for (int i = 0; i < siz; i++)
obj2[i] = B (10, "pranjal");
std::cout << obj2[0].get_nn () << "sd";
}
B::B (int a, string b)
{
bbb = a;
name = b;
}
int main ()
{
A *obj = new A[3];
getdetails (obj);
B *obj2 = new B[3];
for (int i = 0; i < 3; i++) {
obj2[i] = B (obj[i]);
}
create (obj2, 3);
dis (obj2);
obj2->display ();
return 0;
}
void create(B *&obj2,int siz)
{
std::cout<<obj2[0].get_nn(); //this will run fine
for(int i=0;i<siz;i++)
obj2[i]=B(10,"pranjal");
std::cout<<obj2[0].get_nn(); // same line printing again, this will not give output
}
当然不会: B(int, std::string)
不会显式调用A
的构造函数,因此会调用A
的默认构造函数。 这也隐式调用name
的默认构造函数,这将创建一个空字符串。
出于类似的原因,您会得到垃圾值:您的默认构造函数没有为rool
,因此未初始化; 它将保持在 memory 之前的值,无论它是什么,这就是你认为的“垃圾”。
这是因为使用obj2[i] = B(10,"pranjal")
创建了一个新的 object,然后将其复制到目标 object 中(经过优化,可以直接就地创建,但显然不能依赖)。
修复A
的构造函数以将值分配给rool
并且问题应该 go 消失。 您可以选择一个非零值以获得更清晰可见的效果:
A() : rool(1977) { }
或者,您可以为 rool 提供默认值(需要 C++11):
int rool = 1977;
这会很方便,特别是如果你有更多的构造函数。
一般来说:你不应该让你的任何对象的原始类型未初始化以避免类似的问题。
在create
中,您使用 2 个参数构造函数将之前的 B 对象完全替换为全新的对象......这只会使 A object 成员默认初始化。
可以做些什么来修复?
将 B 的参数传递给 A ctor:
B::B (int a, string b): object(a, b) { bbb = a; name = b; }
这样您就可以使用与其父级相同的参数来初始化A object
成员
将A object member
重置为其先前的值:
void create (B * &obj2, int siz) { std::cout << obj2[0].get_nn (); //this will run fine for (int i = 0; i < siz; i++) { A old = obj2[i].object; obj2[i] = B (10, "pranjal"); //this will also run fine obj2[i].object = old; } std::cout << obj2[0].get_nn (); }
但由于它需要访问私有成员,因此create
应在B
class 中声明为friend
:
class B {... friend void create (B * &obj2, int siz); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.