Copy an object and make both share a member variable (C++) - c++

I have been thinking and searching this but I can't solve this question.
I would like an object that when copied into another object, both objects share certain member variable. So, when I change the value of the member variable of object1, it's also changes the variable in object2. Example:
class ABC {
public:
int a = 5;
//...
}
int main() {
ABC object1;
ABC object2 = object1;
object2.a = 7; // now, object1.a is equal to 7
object1.a = 10; // now, object2.a is equal to 10
}
I know about copy constructors, but I am not sure if it applies here or there is
a better method. I have been thinking about using pointers or references, but can't make the trick.
Note that I don't want all the objects to share the same variable.

What you need is a pointer. The pointer points to the object and then all objects that copy the first one just copy the pointer so that they all point to the same thing. To make life easy we can use a std::shared_ptr to manage the allocation and deallocation for us. Something like:
#include <memory>
class Foo
{
private:
std::shared_ptr<int> bar;
public:
Foo() : bar(std::make_shared<int>()) {}
int& getBar() { return *bar; }
};
int main()
{
Foo a;
a.getBar() = 7;
Foo b = a;
b.getBar() = 10;
// now getBar returns 10 for both a and b
Foo c;
// a and b are both 10 and c is 0 since it wasn't a copy and is it's own instance
b = c;
// now b and c are both 0 and a is still 10
}

Related

Temporary objects in C and C++

C code
#include <stdio.h>
typedef struct
{
int a;
}A;
int main()
{
A(); // this line gives error
return 0;
}
Output
Error: Expected identifier or '('
C++ code
#include <iostream>
struct A
{
int a;
A()
{
std::cout<<"Ctor-A\n";
}
~A()
{
std::cout<<"Dctor-A\n";
}
};
int main()
{
A(); // creates temporary object and destroyed it
return 0;
}
Output
Ctor-A
Dctor-A
I know about the "Rule of three", but code becomes complicated and most compilers don't give errors if we don't follow the rule. So I avoided creation of a copy constructor and an overloaded assignment operator.
Why does A()/A{} create a temporary object in C++, but not in C? What's another way to create a temporary object in C?
In C (C99 and later) you can create a structure with automatic lifetime using a Compound Literal.
The syntax is (A){ initializers, for, struct, members }.
The lifetime is automatic, not temporary, so the structure created in this way does not vanish at the end of the full-expression, but at the end of the enclosing scope.
This expression
A()
is considered by C compilers as a function call. In C++ such an expression means a call of a constructor provided that A is a class type.
To create a temporary object in C you could declare a function like for example
struct A { int a; } A( int x )
{
struct A a = { .a = x };
return a;
}
and then you can call the function creating a temporary object of the type struct A like
A( 10 );
Here is a demonstrative program.
#include <stdio.h>
struct A { int a; } A( int x )
{
struct A a = { .a = x };
return a;
}
int main(void)
{
struct A a = A( 10 );
printf( "a.a = %d\n", a.a );
return 0;
}
The program output is
a.a = 10
A() calls the constructor for the struct A in the C++ code. However, C doesn't support constructors / destructors, or even objects (in the sense of OOP) in fact. The only way to create a temporary object as you describe in C would be to declare a variable of type A and wait for it to go out of scope, where the memory allocated for it will be popped from the stack, or to allocate a variable on the heap and free it in the next line.

Adding objects to a pre allocated array of objects

#include <iostream>
class Object {
public:
int x;
Object() { }
Object(int x) {
this->x = x;
}
};
class SomeClass {
public:
Object array[10];
int n;
SomeClass() { n = 0; }
void insert(Object o) {
array[n++] = o;
}
};
int main() {
SomeClass s;
Object test = Object(4);
s.insert(test);
return 0;
}
I have this example where I pre-allocate an array of objects in SomeClass and then in some other method, main in this example, I create another object and add it to the array in SomeClass
One thing I think I should do is to switch from array[10] to an array of pointers so that I only create objects when I really need to.
But my question is what happens to the memory originally allocated for array[0] when I do "array[n++] = o" replacing it by the new object "o"? does it get de-allocated?
No, nothing happens. The object's assignment operator gets invoked, to replace the object.
For this simple class, the assignment operator is the default operator which, more or less, copies each of the object's members, one at a time.
(No need to get into move operators, etc..., just yet)

Assigning structs including pointers

Consider the following simple program. It just defines two variables A and B from MyStruct and then initializes A.
How can I copy A to B with new pointers for B?
If I use assignment operator, firstMember of B will be assigned with firstMember of A and whenever I change the value of B.firstMember[i], the value of A.firstMember[i] will change. I already know that it can be done with a function, but is there any simpler way? Assuming that MyStruct can have lots pointers, writing a function doesn't seem to be good.
typedef struct MyStruct
{
int * firstMember;
int secondMember;
} MyStruct;
int main()
{
MyStruct A;
MyStruct B;
Initialize(A); // initializes A such that A.firstMember[0] != 5
B = A;
B.firstMember[0] = 5;
cout<<A.firstMember[0]; // prints 5
return 0;
}
This falls into the "Rule of 3". Once you start managing raw pointers inside a class, you probably want to define a custom contructor, destructor, and assignment operator for this very reason.
First of all, I don't understand why among two members, one is a pointer, and other is not. I am presuming that the first member which is a pointer points to a single integer. Here's an improved code snippet for the structure:
struct MyStruct
{
int * firstMember;
int secondMember;
MyStruct& operator = (const MyStruct& src) {
*firstMember = *src.firstMember;
return *this;
}
MyStruct(int a = 0) {
firstMember = new int(a);
secondMember = a;
}
MyStruct(const MyStruct& src) {
// deep copy
firstMember = new int(*src.firstMember);
secondMember = src.secondMember;
}
~MyStruct() {
if (firstMember)
delete firstMember;
}
};
Note, struct is a class with all members declared as public by default. Rather than having Initialize in C-way, better to use C++ proper construct like constructor, and you need to have copy constructor and assignment operator too.

copy on write using a pointer on integer

I try to implement copy on write using a pointer on integer. But I don't understand how to write the code. The idea is very clear in my head: when I use the default constructor, I create a new instance of the object (number of instances=1) and when I use the copy constructor, I increment the number of instances and make a shallow copy of the object.
class Myclass
{
public:
Myclass(const char * foo, int foo2) : foo(foo), foo2(foo2)
{
(*ref)=1;
}
Myclass(const Myclass& rhs) :foo(rhs.foo),foo2(rhs.foo2)
{
(*ref)++;
}
const char * foo;
int foo2;
int *ref;
};
I begin with C++ and the notion of pointer is completely news for me so I tried this.
But I really don't understand why "ref" is still equal to 1 even if I create a copy of the object witht he copy constructor.
Your default constructor needs to create a new reference count:
ref(new int(1))
And your copy constructor needs to make the new object end up with a pointer to the original object's reference count and to increment it (which you already do):
ref(rhs.ref)
I'm having trouble understanding what you want but I'll try...
I create Myclass foo ("foo",10) and then Myclass foo2(foo). I want
all ref to be equal to 2. Here only the ref of foo2 is equal to 2.
The ref of foo is equal to 1. I think I need a pointer, no?
This can be accomplished with a static variable:
class Myclass
{
public:
Myclass(const char * foo, int foo2) : foo(foo), foo2(foo2)
{
ref += 1;
if (1 == ref) {} // First ref, do something?
}
Myclass(const Myclass& rhs) :foo(rhs.foo),foo2(rhs.foo2)
{
ref += 1;
}
~Myclass() // Decrement on delete
{
ref -= 1;
if (0 == ref) {} // Last reference. Do something?
}
const char * foo;
int foo2;
static int ref;
};
int Myclass::ref = 0; // Initialize to 0
Then....
Myclass foo("foo",10); // ref becomes 1
Myclass foo2(foo); // ref becomes 2
Myclass *foo3 = new Myclass(foo); // ref becomes 3
delete foo3; // ref becomes 2

Scope of variables in if statements

I have a class that has no default constructor or assignment operator so it is declared and initialized within an if/else statement depending on the result of another function. But then it says that it is out of scope later even though both routes of the conditional will create an instance.
Consider the following example (done with int just to illustrate the point):
#include <iostream>
int main()
{
if(1) {
int i = 5;
} else {
int i = 0;
}
std::cout << i << std::endl;
return 0;
}
Do variables declared in a conditional go out of scope at the end of the conditional? What is the correct way to handle the situation where there is no default constructor but the arguments for the constructor depend on certain conditionals?
Edit
In light of the answers given, the situation is more complex so maybe the approach would have to change. There is an abstract base class A and two classes B and C that derive from A. How would something like this:
if(condition) {
B obj(args);
} else {
C obj(args);
}
change the approach? Since A is abstract, I couldn't just declare A* obj and create the appropriate type with new.
"Do variables declared in a conditional go out of scope at the end of the conditional?"
Yes - the scope of a local variable only falls within enclosing brackets:
{
int x; //scope begins
//...
}//scope ends
//x is not available here
In your case, say you have class A.
If you're not dealing with pointers:
A a( condition ? 1 : 2 );
or if you're using a different constructor prototype:
A a = condition ? A(1) : A(2,3);
If you're creating the instance on the heap:
A* instance = NULL;
if ( condition )
{
instance = new A(1);
}
else
{
instance = new A(2);
}
or you could use the ternary operator:
//if condition is true, call A(1), otherwise A(2)
A* instance = new A( condition ? 1 : 2 );
EDIT:
Yes you could:
A* x = NULL; //pointer to abstract class - it works
if ( condition )
x = new B();
else
x = new C();
EDIT:
It seems what you're looking for is the factory pattern (look it up):
class A; //abstract
class B : public A;
class C : public A;
class AFactory
{
public:
A* create(int x)
{
if ( x == 0 )
return new B;
if ( x == 1 )
return new C;
return NULL;
}
};
Do variables declared in a conditional go out of scope at the end of
the conditional?
Yes.
What is the correct way to handle the situation where there is no
default constructor but the arguments for the constructor depend on
certain conditionals?
Write a function that returns a value, from which you copy.
T foo()
{
if(condition)
return T(x);
return T(y);
}
void bar()
{
T i(foo());
}
Edit:
Since A is abstract, I couldn't just declare A* obj and create the
appropriate type with new.
What do you mean? That's exactly how dynamic typing works. Except I wouldn't use a raw pointer, I would use a unique_ptr.
std::unique_ptr<A> obj;
if(condition) {
obj = std::unique_ptr<A>(new B(args));
} else {
obj = std::unique_ptr<A>(new C(args));
}
Your alternative will be pointers:
MyObject *obj;
if(cond1)
{
obj = new MyObject(1, 2, 3);
}
else
{
obj = new MyObject(4, 5);
}
Remember to delete it when you are done with it, or use a smart pointer.
Yes it will be out of scope if declared in a conditional, loop etc. Will the type of the variable change depending on the conditional?