Two different objects acting the same way

Hi everyone,

I have some problem and really hope that someone will help me :)
I have pointers to objects of the same class. Lets assume that every object has property name.

The question is - Can I have two objects with different names(i.e. two different objects) and make them act as if they were the same object?

Of course I can do it manually in my program,but I wonder does C++ give some way to get this result automatically?

Thanks.
This is not clear. If you have a function in the class which returns the name, they will return different strings. In other words, they are going to behave differently. Is this what you want to avoid or something else?
have you ever heard of a reference to an object? Making a reference to an object and editing the object via the reference will change the original, since a reference is just an alias for that object. Observe.

1
2
3
4
5
6
7
8
9
int main()
{
    int x = 5;
    int &y = x; // y is a reference to x
    y = 10;

    std::cout << "x = " << x << "\n";
    std::cout << "y = " << y << "\n";
}


would output

x = 10
y = 10


Is that what you're after?
Last edited on
No, No friends!

Of course I know about reference, but in this case we will have one object with two different names, e.g. in above example y is not a different object, it is just second name (alias) for x.
What I want is this

1
2
3
4
5
6
7
8
9
class A
{
private: 
std::string name;
...........................
}

...........................
std::list<A*> listA;


I have lots of pointers to objects of class A in listA container and I want two of them act the same way...How can I design my project to get such a result - To have two different objects(which have different names), and when I do something with one of them, the same be done for other one.

The most important think for me is that they must have different names.Of course I can let my object to have 2 names, and I just will have one object with two different names, but can't use this method.

Thanks again.
Reclarifying ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class C
{
	public:
	void clear (void)
	{
		i = 0;
	}

	private:
	int i;
};

int main (void)
{
	C o1, o2;
	o1.clear (); // do you want that o2.clear be automatically called?
}

Yes krishnendu, exactly!
And not only that.I want them to act like there were the same object, just with different names.
Last edited on
I want them to act like there were the same object, just with different names.
What's the difference between what you want and references? References are a different name for the same object
If you are working with pointers what you need are pointers to the same object
I would suggest breaking the class into 2 parts, an Implementation and a Wrapper. The Implementation class would contain all of the functionality, while the Wrapper would contain the name and a pointer or reference to an Implementation. The identical objects would both point or refer to the same Implementation, while unique objects would point or refer to unique Implementations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Implementation
{
public:
	Implementation(int val) : i(val) { }
	void clear() { i = 0; }

private:
	int i;
};

class Wrapper
{
public:
	Wrapper(Implementation& impl) : _impl(impl) { }
	void clear() { _impl.clear(); }
private:
	Implementation& _impl;
};

int main()
{
	Implementation twinImpl(1);
	Implementation uniqueImpl(2);
	Wrapper twin1(twinImpl);
	Wrapper twin2(twinImpl);
	Wrapper unique(uniqueImpl);

	twin1.clear();	// twin1 and twin2 both have Implementations with i = 0;
			// unique has Implementation with i = 2
}
Last edited on
If you want that a specific method is to be called for all instances of the class if it is called for any one of them, then I have the following solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <list>
#include <string>

using namespace std;

/*****************************************************************************/

class C
{
	public:
	C (const string &name);

	public:
	~C (void);

	public:
	void foo (void);

	private:
	void bar (void)
	{
		cout << "Method 'bar' is called by " << name << endl;
	}

	private:
	static list<C *> objects;
	string name;
};

/*****************************************************************************/

list<C *> C::objects;

C::C (const string &name)
{
	this->name = name;
	objects.push_back (this);
}

C::~C (void)
{
	objects.remove (this);
}

void C::foo (void)
{
	list<C *>::iterator it;
	cout << "Method 'foo' is called by " << name << endl;

	for (it = objects.begin (); it != objects.end (); it++)
		(*it)->bar ();
}

/*****************************************************************************/

int main (void)
{
	C o1 ("sisi");
	C o2 ("krishnendu");
	C o3 ("quirkyusername");
	C o4 ("Bazzy");
	C o5 ("doug4");

	o1.foo ();
}


The output will be:


Method 'foo' is called by sisi
Method 'bar' is called by sisi
Method 'bar' is called by krishnendu
Method 'bar' is called by quirkyusername
Method 'bar' is called by Bazzy
Method 'bar' is called by doug4


Hope it answers your question. Enjoy coding :)
Thank you all for your kind replays.
Maybe I just couldn't clearly describe the situation where I'm in.
But anyway, I have one way, to let objects to have two or more names.

Again thank you all for trying to help me.
Topic archived. No new replies allowed.