473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

ReRef : Reseatable Reference

Having decided not to use macros at all in designing my reseatable
references, and having looked through the list of overloadable operators and
combinations of them, I've settled on the following syntax:

Reseat >> kref = moon;

Where "kref" is the reseatable reference and "moon" is the new object it'll
refer to.

Overall, once a reseatable reference has been defined, I want it to act
exactly as would a normal reference... but so far it doesn't seem like I'll
be able to achieve this, especially since I won't be able to access members
after the dot.

Anyway, to achieve the above syntax, I've declared a global object called
"Reseat" (sort of like "cout"). This "Reseat" global object is of type
"Reseat_Class". The "Reseat_Class" class has an operator>> defined which
returns by value an intialized object of type "RefReseater". The class
"RefReseater" is a friend of the "ReRef" and so has access to its "Reseat"
member function (not to be confused with the "Reseat" global object). The
"RefReseater" class has operator= defined which changes what's referred to.
Here's the code so far:

template<class T> class RefReseater;

template<class T>
class ReRef
{
private:

T* p_t;

ReRef& Reseat(T& in)
{
p_t = &in;

return *this;
}

public:

explicit ReRef() : p_t(0) {}

explicit ReRef(T& in)
{
Reseat(in);
}

/*
T& operator. ()
{
return *p_t;
}
*/

operator T&()
{
return *p_t;
}

friend class RefReseater<T>;
};

class Reseat_Class
{
public:

template<class T>
RefReseater<T> operator>> (ReRef<T>& in) const
{
return RefReseater<T>(in);
}
} Reseat;

template<class T>
class RefReseater
{
private:

ReRef<T>& reref_object;

public:

RefReseater(ReRef<T>& in) : reref_object(in)
{

}

ReRef<T>& operator= (T &object_in)
{
return reref_object.Reseat(object_in);
}
};
int main()
{
ReRef<int> j;

int a = 1;
int b = 2;
int c = 3;
int d = 4;

Reseat >> j = a;

//j = 9;

Reseat >> j = b;

//j = 8;

Reseat >> j = c;

//j = 7;

Reseat >> j = d;

//j = 6;
};
Any comments, questions, suggestions welcomed.
-JKop
Jul 22 '05 #1
8 1650

"JKop" <NU**@NULL.NULL> wrote in message
news:Kn******************@news.indigo.ie...
Having decided not to use macros at all in designing my reseatable
references, and having looked through the list of overloadable operators and combinations of them, I've settled on the following syntax:

Reseat >> kref = moon;

Where "kref" is the reseatable reference and "moon" is the new object it'll refer to.

Overall, once a reseatable reference has been defined, I want it to act
exactly as would a normal reference... but so far it doesn't seem like I'll be able to achieve this, especially since I won't be able to access members after the dot.

Anyway, to achieve the above syntax, I've declared a global object called
"Reseat" (sort of like "cout"). This "Reseat" global object is of type
"Reseat_Class". The "Reseat_Class" class has an operator>> defined which
returns by value an intialized object of type "RefReseater". The class
"RefReseater" is a friend of the "ReRef" and so has access to its "Reseat"
member function (not to be confused with the "Reseat" global object). The
"RefReseater" class has operator= defined which changes what's referred to. Here's the code so far:

template<class T> class RefReseater;

template<class T>
class ReRef
{
private:

T* p_t;

ReRef& Reseat(T& in)
{
p_t = &in;

return *this;
}

public:

explicit ReRef() : p_t(0) {}

explicit ReRef(T& in)
{
Reseat(in);
}

/*
T& operator. ()
{
return *p_t;
}
*/

operator T&()
{
return *p_t;
}

friend class RefReseater<T>;
};

class Reseat_Class
{
public:

template<class T>
RefReseater<T> operator>> (ReRef<T>& in) const
{
return RefReseater<T>(in);
}
} Reseat;

template<class T>
class RefReseater
{
private:

ReRef<T>& reref_object;

public:

RefReseater(ReRef<T>& in) : reref_object(in)
{

}

ReRef<T>& operator= (T &object_in)
{
return reref_object.Reseat(object_in);
}
};
int main()
{
ReRef<int> j;

int a = 1;
int b = 2;
int c = 3;
int d = 4;

Reseat >> j = a;

//j = 9;

Reseat >> j = b;

//j = 8;

Reseat >> j = c;

//j = 7;

Reseat >> j = d;

//j = 6;
};
Any comments, questions, suggestions welcomed.
-JKop


It doesn't work! One of the important things about having a reference is
you
can be somewhat sure that its refering to a valid object , however, in the
following
code, the line int z = i; crashes because i doesn't represent anything, you
have to
manually set i before it an be used.

Also, there was no way to initialize values using the construct such as
ReRef<int> i(0); or ReRef<int> i=0, the compiler wouldn't accept it.

dave
#include <string>
using namespace std;

template<class T> class RefReseater;

template<class T>
class ReRef
{
private:

T* p_t;

ReRef& Reseat(T& in)
{
p_t = &in;

return *this;
}

public:

explicit ReRef() : p_t(0) {}

explicit ReRef(T& in)
{
Reseat(in);
}

/*
T& operator. ()
{
return *p_t;
}
*/

operator T&()
{
return *p_t;
}

friend class RefReseater<T>;
};

class Reseat_Class
{
public:

template<class T>
RefReseater<T> operator>> (ReRef<T>& in) const
{
return RefReseater<T>(in);
}
} Reseat;

template<class T>
class RefReseater
{
private:

ReRef<T>& reref_object;

public:

RefReseater(ReRef<T>& in) : reref_object(in)
{

}

ReRef<T>& operator= (T &object_in)
{
return reref_object.Reseat(object_in);
}
};
int main()
{
ReRef<string> j ;
ReRef<string> k ;
ReRef<char> ch ;
ReRef<int> i ;

int z = i;

string a = "into";
string b = "the";
string c = "valley";
string d = "ofdeath";

Reseat >> j = a;

string x = j;

Reseat >> j = b;

x = j;

Reseat >> j = c;

x = j;

Reseat >> j = d;
x = j;

return 0;

};

Jul 22 '05 #2
It doesn't work!

It's not complete! That's only a draft.
-JKop
Jul 22 '05 #3
"JKop" <NU**@NULL.NULL> wrote in message
news:Kn******************@news.indigo.ie...
[snip]


It's a noble goal. I'm not exactly sure if it's really -that- useful, but I
admire you for trying. I wonder if it is really possible to do it, given
enough work?
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL> wrote in message
news:Kn******************@news.indigo.ie...

Just curious (perhaps you've mentioned it elsewhere before)...

Why do you want a "reseatable" reference? You have pointers which are
assignable to other objects. Why do you need to be able to re-assign
references too? What advantage over pointers would it provide? (Especially
given the pointer-like problems that might be introduced!)

-Howard

Jul 22 '05 #5
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:Kn******************@news.indigo.ie...

Just curious (perhaps you've mentioned it elsewhere before)...
Why do you want a "reseatable" reference? You have pointers which are assignable to other objects. Why do you need to be able to re-assign references too? What advantage over pointers would it provide? (Especially given the pointer-like problems that might be introduced!)
-Howard


Consider you have a business. There's an owner, a
chairperson, 3 managers and 12 employees.

class Business
{
public:

Owner& owner;
Chairperson& chairperson;
Manager& manager[3];
Employee& employees[12];

Business(Owner& in_owner, ...) : owner(in_owner),...
};
People come and go. Maybe one day one of the empoyees gets
hit by a bus. Or a manager gets fired for bullying
employees. The first solution would be pointers:

class Business
{
public:

Owner* p_owner;
Chairperson* p_chairperson;
Manager* p_manager[3];
Employee* p_employees[12];

Business(Owner* in_p_owner, ...) : p_owner
(in_p_owner),...
};
While there's nothing wrong with it, I want it to be nicer
and more simplistic than that, as it is in Visual Basic.
With the pointer version, you'll always have to use the
asterisk:

int main()
{
...

Business k(....);

k.*p_owner.AnnouncePressConference();

//owner gets killed

k.p_owner = Owner();

k.*p_owner.AnnounceNewOwner();
}

In its place, I'd prefer:

int main()
{
....

Business k(....);

k.owner.AnnouncePressConference();

//owner gets killed

Reseat >> k.owner = Owner();

k.owner.AnnounceNewOwner();
}
Or something along those lines.
-JKop
Jul 22 '05 #6

"JKop" <NU**@NULL.NULL> wrote in message
news:vK******************@news.indigo.ie...
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:Kn******************@news.indigo.ie...

Just curious (perhaps you've mentioned it elsewhere

before)...

Why do you want a "reseatable" reference? You have

pointers which are
assignable to other objects. Why do you need to be able

to re-assign
references too? What advantage over pointers would it

provide?
(Especially given the pointer-like problems that might be

introduced!)

-Howard


Consider you have a business. There's an owner, a
chairperson, 3 managers and 12 employees.

class Business
{
public:

Owner& owner;
Chairperson& chairperson;
Manager& manager[3];
Employee& employees[12];

Business(Owner& in_owner, ...) : owner(in_owner),...
};
People come and go. Maybe one day one of the empoyees gets
hit by a bus. Or a manager gets fired for bullying
employees. The first solution would be pointers:

class Business
{
public:

Owner* p_owner;
Chairperson* p_chairperson;
Manager* p_manager[3];
Employee* p_employees[12];

Business(Owner* in_p_owner, ...) : p_owner
(in_p_owner),...
};
While there's nothing wrong with it, I want it to be nicer
and more simplistic than that, as it is in Visual Basic.
With the pointer version, you'll always have to use the
asterisk:

int main()
{
...

Business k(....);

k.*p_owner.AnnouncePressConference();

//owner gets killed

k.p_owner = Owner();

k.*p_owner.AnnounceNewOwner();
}

In its place, I'd prefer:

int main()
{
...

Business k(....);

k.owner.AnnouncePressConference();

//owner gets killed

Reseat >> k.owner = Owner();

k.owner.AnnounceNewOwner();
}
Or something along those lines.
-JKop

Jul 22 '05 #7

"JKop" <NU**@NULL.NULL> wrote in message
news:vK******************@news.indigo.ie...
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:Kn******************@news.indigo.ie...

Just curious (perhaps you've mentioned it elsewhere before)...

Why do you want a "reseatable" reference? You have

pointers which are
assignable to other objects. Why do you need to be able

to re-assign
references too? What advantage over pointers would it

provide?
(Especially given the pointer-like problems that might be

introduced!)

-Howard


Consider you have a business. There's an owner, a
chairperson, 3 managers and 12 employees.

class Business
{
public:

Owner& owner;
Chairperson& chairperson;
Manager& manager[3];
Employee& employees[12];

Business(Owner& in_owner, ...) : owner(in_owner),...
};
People come and go. Maybe one day one of the empoyees gets
hit by a bus. Or a manager gets fired for bullying
employees. The first solution would be pointers:

class Business
{
public:

Owner* p_owner;
Chairperson* p_chairperson;
Manager* p_manager[3];
Employee* p_employees[12];

Business(Owner* in_p_owner, ...) : p_owner
(in_p_owner),...
};
While there's nothing wrong with it, I want it to be nicer
and more simplistic than that, as it is in Visual Basic.
With the pointer version, you'll always have to use the
asterisk:

int main()
{
...

Business k(....);

k.*p_owner.AnnouncePressConference();


Is that legal syntax? It looks strange to me, using the dereference
operator after the member (.) operator.
If you choose to dereference, shouldn't it be

*(k.p_owner).AnnouncePressConference();

? But, why dereference? Why not:

k.p_owner->AnnouncePressConference();

That's the usual pointer syntax.

Or better still, k.AnnouncePressConference();

where Business announces the press conference, possibly via a call on its
Owner member. I almost never call a function belonging to another object's
member, preferring to let the containing object decide who should handle
such details for me. Also, that way I can have private or protected members
if I so desire.

//owner gets killed

k.p_owner = Owner();
I'm not sure what you meant here. Owner is a type, right? But here you're
assigning it to a pointer-to-type.

k.*p_owner.AnnounceNewOwner();
Again, why not use the -> operator?
}

In its place, I'd prefer:

int main()
{
...

Business k(....);

k.owner.AnnouncePressConference();

//owner gets killed

Reseat >> k.owner = Owner();

k.owner.AnnounceNewOwner();
}
Or something along those lines.
-JKop


I fail to see any advantage here at all. One less character (*) to type?
Surely that's not you sole motivation for this!?!

(Not to mention, I think your Owner is not going to be happy that you've
hard-coded his impending death like that! :-))

-Howard

Jul 22 '05 #8
JKop wrote:
Howard posted:

"JKop" <NU**@NULL.NULL> wrote in message
news:Kn******************@news.indigo.ie...

Just curious (perhaps you've mentioned it elsewhere
before)...
Why do you want a "reseatable" reference? You have


pointers which are
assignable to other objects. Why do you need to be able


to re-assign
references too? What advantage over pointers would it


provide?
(Especially given the pointer-like problems that might be


introduced!)
-Howard

Consider you have a business. There's an owner, a
chairperson, 3 managers and 12 employees.

class Business
{
public:

Owner& owner;
Chairperson& chairperson;
Manager& manager[3];
Employee& employees[12];

Business(Owner& in_owner, ...) : owner(in_owner),...
};
People come and go. Maybe one day one of the empoyees gets
hit by a bus. Or a manager gets fired for bullying
employees. The first solution would be pointers:

class Business
{
public:

Owner* p_owner;
Chairperson* p_chairperson;
Manager* p_manager[3];
Employee* p_employees[12];

Business(Owner* in_p_owner, ...) : p_owner
(in_p_owner),...
};
While there's nothing wrong with it, I want it to be nicer
and more simplistic than that, as it is in Visual Basic.
With the pointer version, you'll always have to use the
asterisk:

int main()
{
...

Business k(....);

k.*p_owner.AnnouncePressConference();


A better syntax in overall:

k.pOwner->AnnouncePressConference();

//owner gets killed

k.p_owner = Owner();

That doesn't look right.Perhaps you mean:
k.pOwner = new Owner;

k.*p_owner.AnnounceNewOwner();

k.pOwner->AnnounceNewOwner();


}

In its place, I'd prefer:

int main()
{
...

Business k(....);

k.owner.AnnouncePressConference();

//owner gets killed

Reseat >> k.owner = Owner();

k.owner.AnnounceNewOwner();
}
Or something along those lines.


VB "reseating references" as you mentioned them, are pointers with the
difference that instead of -> they use . (and not being able to do
pointer arithmetic etc).
Does it really bother you to use ->?

This one is the answer to your question:
http://www.research.att.com/~bs/bs_f...and-references


Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
34
by: JKop | last post by:
The other day I had to write some code to manipulate a Micrsoft Excel spreadsheet. I had to write it in Visual Basic, which I haven't used for about 5 years. Anyway, it slowly started creeping back...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.