Is it possible to have pointers to references in c++? I'm asking because in my simple code:

#include "stdafx.h"
#include "Nunu.h"

int _tmain(int argc, _TCHAR* argv[])
{
Nunu* p = new Nunu[5];
Nunu a("a");
Nunu b("b");
Nunu c("c");
p[0] = a; //critical line/s
p[1] = b; //
p[2] = c; //
for (int i = 0; i < 3; ++i)
{
cout << p->name() << '\n';
}
	return 0;
}
////////class Nunu
#pragma once 
#include "stdafx.h"

class Nunu
{
private:
	const string& _my_name;
public:
	Nunu():_my_name("UNNAMED"){};
	Nunu(const string& the_name):_my_name(the_name)
	{
	}
	string name()const
	{
		return _my_name;
	}
};

I'm getting this message:
"Error 2 error C2582: 'operator =' function is unavailable"
which would suggest that in "critical lines" assignment is taking place instead of setting out pointer. But when I change in class Nunu _my_name from reference to pointer this whole code works. Why?

//class Nunu after applying those changes
#pragma once 
#include "stdafx.h"

class Nunu
{
private:
	const string* _my_name; //now pointer<-------------------------------------------------
public:
	Nunu():_my_name(nullptr){};
	Nunu(const string& the_name):_my_name(&the_name)
	{
	}
	string name()const
	{
		return *_my_name;
	}
};

after this little change rest of the code works as intended (in main when constructing those object I obviously have to provide pointer to string instead of ref to string);
Thank you for any reply.

Edited 7 Years Ago by atch: n/a

Here, you are attempting to assign a 'Nunu' object to another 'Nunu' object:

p[0] [B]=[/B] a;

//"Error 2 error C2582: 'operator =' function is unavailable"

Assignment among primitive data types, such as int, float, char, double, etc. is handled by the compiler with relative ease. But what happens when you try to assign data types of your own design?

Fortunately, there is a method available that will allow you the programmer to define how your aggregate data types will respond to various operations: Operator Overloading.

Here is a small example that illustrates how 'point' objects can be assigned to one another:

// assignment.cpp
class Point
{
public:
   Point &operator=( Point & );  // Right side is the argument.
   int _x, _y;
};

// Define assignment operator.
Point &Point::operator=( Point &ptRHS )
{
   _x = ptRHS._x;
   _y = ptRHS._y;

   return *this;  // Assignment operator returns left side.
}

In the above scenario, the x and y coordates are dereferrenced from the 'right side object' (the source object) and assigned to the x and y coordates to the 'left-hand side.' The compiler knows how to handle assignment between two 'points' objects.

point p1, p2, p3;

p1 [B]=[/B] p2;
p2 [B]=[/B] p3;
//overloaded assignment operators at work, handling their 'left-hand side' and 'right-hand side' arguments

Operator overloading give the programmer great flexibility to assign user-defined objects to other user-defined objects. The assignment operator (=) in my opinion is probably the easiest operator to overload. With a few simple adjustments, you'll be able to assign 'Nunu's to other 'Nunu's with relative ease.

Edited 7 Years Ago by Clinton Portis: n/a

Here, you are attempting to assign a 'Nunu' object to another 'Nunu' object:

p[0] [B]=[/B] a;[

//"Error 2 error C2582: 'operator =' function is unavailable"

Assignment among primitive data types, such as int, float, char, double, etc. is handled by the compiler with realative ease. But what happens when you try to assign data types of your own design?

Fortunatly, there is a method available that will allow you the programmer to define how your aggregate data types will respond to various operations: Operator Overloading.

Here is a small example that illustrates how 'point' objects can be assigned to one another:

// assignment.cpp
class Point
{
public:
   Point &operator=( Point & );  // Right side is the argument.
   int _x, _y;
};

// Define assignment operator.
Point &Point::operator=( Point &ptRHS )
{
   _x = ptRHS._x;
   _y = ptRHS._y;

   return *this;  // Assignment operator returns left side.
}

The compiler knows how to handle assignment (=) between two 'points' objects.

Operator overloading give the programmer great flexibility to assign user-defined objects to other user-defined objects. The assignment operator (=) in my opinion is probably the easiest operator to overload. With a few simple adjustments, you'll be able to assign 'Nunu's to other 'Nunu's with relative ease.

No, No... I know that compiler wants to assign Nunu to Nunu but only when Nunu's _my_name member is a reference - after I've change _my_name from ref to pointer line p[0] = a; works and compiler doesn't want any more assign Nunu to Nunu it's just setting p[0] to be "directed" to "a" which is a Nunu object. You just completely misunderstood my question.
And another rather strange behavior:
- if I remove default ctor from Nunu this line doesn't work anymore:

Nunu* p = new Nunu[5];

I'm getting this msg:
"Error 1 error C2512: 'Nunu' : no appropriate default constructor available"
Why? Does it mean that I cannot allocate on heap objects without dflt ctors?
So just to summarize. My 3 main questions are:
1. Can we have pointers to references?
2. Why when I change member in my Nunu class from ref to pointer line p[0] = a; works
3. Why when I remove dflt ctor from Nunu line Nunu* p = new Nunu[5]; doesn't work.
Thank you

Edited 7 Years Ago by atch: n/a

>>Is it possible to have pointers to references in c++

Yes its possible, long as you watch out for const.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{  
	int X = 4;
	int &y = X;
	int *p_to_y = &y;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl << endl;
	
	X = 10;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl << endl;

	y = 15;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl <<endl;

	*p_to_y = 20;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl;
	
}

>>Is it possible to have pointers to references in c++

Yes its possible, long as you watch out for const.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{  
	int X = 4;
	int &y = X;
	int *p_to_y = &y;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl << endl;
	
	X = 10;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl << endl;

	y = 15;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl <<endl;

	*p_to_y = 20;

	cout<<"X is " << X<<endl;
	cout<<"Y is " << y<<endl;
	cout<<"p_to_y is " << *p_to_y << endl;
	
}

Just found out that pointers to ref are not allowed. What you doing is you are assigning address of var x through ref to this variable. Nothing to do whit pointers to ref. If this notion would be a proper c++ you should be able to declare:

int&* p; // p is a pointer to int's ref

but this isn't correct c++ because:
POINTERS TO REFERENCES ARE NOT ALLOWED IN C++.
As I just answered myself to my first question I would also like to mention that would be nice if answers on this forum would be given if one is really sure of it (explanation in a style "I thought it is correct" or "I'm sorry my mistake" is good for primary school" not for forum like this) not just guessing and misinterpreting c++ syntax and notion of it.

So just to summarize. My 3 main questions are:
1. Can we have pointers to references?//No we can't - answered to it myself
2. Why when I change member in my Nunu class from ref to pointer line p[0] = a; works
3. Why when I remove dflt ctor from Nunu line Nunu* p = new Nunu[5]; doesn't work.
4. In number 3 as I mentioned construction Nunu* p = new Nunu[5]; doesn't work. As I understand it doesn't work because operator new wants to create object but and this is my fourth question if operator new creates new object is this object temporary or is it placed somewhere? Because I don't think is placed in p[0] or is it? //answer - yes it places it on p[0].
Ok so finally (hope so) fifth question: Is there any way to reserve free store without actually create an object of given class? (Excluding malloc etc) - just plain c++
Thank you

Edited 7 Years Ago by atch: n/a

First off when you have a reference to a Data_Type in a class,
they have to be initialized by the initializer list.
Also what does it mean to have a reference of the following :

int &a = 3;

You are saying that a is a reference to a literal, and thats an error in c++;
But what about const reference :

const int& a = 3;

This is a valid code in c++. C++ allows const reference, but not for
member variables.

With that in mind lets see what you are doing :

class Nunu
{
private:
	const string& _my_name;  //const reference, must be initialized in the initializer list
public:
	Nunu():_my_name("UNNAMED"){}; // BUG, because a reference to a literal , which goes out of scope soon!
	Nunu(const string& the_name):_my_name(the_name) //ok for now as long as the_name is not a literal, because _my_name wont be valid as it will run out of scope.
	{
	}
	string name()const
	{
		return _my_name;
	}
};

int main()
{
   Nunu* p = new Nunu[5]; / ok, but what happens to the string reference?
  Nunu a("a"); //does not work as you might
  Nunu b("b");//see above
  Nunu c("c"); //see above
  p[0] = a; //now you are making a copy from a to p[0], but p[0] takes a const string& ref so this is invalid
  p[1] = b; // see above 
  p[2] = c; // see above
  for (int i = 0; i < 3; ++i)
  {
      cout << p->name() << '\n';
   }
	return 0;
}
}

Moral of the story, don't use reference, it gets complicated.
Why do you need a reference anyway?


POINTERS TO REFERENCES ARE NOT ALLOWED IN C++.
.

Your words are misleading because a pointer-to-a-reference means

int a = 3;
int &r = a;
int *p = &r; //a pointer to a reference

what you really mean :

int x = 3;
int & *a = &x;

Is invalid because in sort a reference is like a pointer, so
it really does not make sense to have a reference and a pointer variable. They both do the same thing, ultimately, so there is no need.

Moral of the story, don't use reference, it gets complicated.
Why do you need a reference anyway?

With full respect but question like this cracks me up. Why do I need reference anyway? How can I answer to that question maybe with another question: Following your logic if something gets to complicated should one just stop using it instead of mastering use of it?
And I'm sorry but why and what for I need references it is entirely my business not yours and if you don't know the proper answer just don't give any answer (especially not asking another question) and stay quiet.

Your words are misleading because a pointer-to-a-reference means

int a = 3;
int &r = a;
int *p = &r; //a pointer to a reference

what you really mean :

int x = 3;
int & *a = &x;

Is invalid because in sort a reference is like a pointer, so
it really does not make sense to have a reference and a pointer variable. They both do the same thing, ultimately, so there is no need.

I don't know what school you finished and what books you've read but after this reply I'm saying that you have very little knowledge of c++ syntax and of c++ as whole.
First:

int *p = &r; //a pointer to a reference

no its not a pointer to a reference. Just read it p is a pointer to int. What you're doing is you assigning an address of variable referenced by r.
And as for your explanation:

Is invalid because in sort a reference is like a pointer, so
it really does not make sense to have a reference and a pointer variable. They both do the same thing, ultimately, so there is no need

what in the opposite way. Why is it ok to have:

int*& p;

but not according to you:

int&* p;

If ref and pointers are according to you this same why second doesn't it make sense and the first one does? As I said:
POINTERS TO REFERENCES ARE NOT ALLOWED IN C++.
And by saying different you're talking absolute rubbish. Go and read more books. Good books. And stop telling your silly theories.

Edited 7 Years Ago by atch: n/a

I don't know what school you finished and what books you've read but after this reply I'm saying that you have very little knowledge of c++ syntax and of c++ as whole.

Whats up with the attitude? I tried to help, but instead you insist that you are an asshole.

First:

int *p = &r; //a pointer to a reference

no its not a pointer to a reference. Just read it p is a pointer to int. What you're doing is you assigning an address of variable referenced by r.

Its a pointer to a int, in which the int is a reference to another variable, thus it follows that p is a pointer to a (int)refrence

And as for your explanation:
what in the opposite way. Why is it ok to have:

How about you fix your grammar before you ask question.

int*& p;

but not according to you:

int&* p;

If ref and pointers are according to you this same why second doesn't it make sense and the first one does?

Again, are you talking English, or are you still in middle school ?

I did not say reference is the same as pointer, I said a pointer is sort of like a reference where when you change the reference value or a pointer value, the original value changes.

int* &a; //reads as a pointer that is a reference //Absurd
int& *b;  //read as a reference that is a pointer  ILLEGAL

why would you even want a pointer that is a reference, there should be no reason at all. You can achieve what you desire else wise.

As I said:
POINTERS TO REFERENCES ARE NOT ALLOWED IN C++.

Again you write what you don't mean.

And by saying different you're talking absolute rubbish. Go and read more books. Good books. And stop telling your silly theories.

Go F yourself. If you can't ask question correctly, then don't.

With full respect but question like this cracks me up. Why do I need reference anyway? How can I answer to that question maybe with another question: Following your logic if something gets to complicated should one just stop using it instead of mastering use of it?

No if something get complicated you should try to find a simpler solution, than writing absurd code. This isn't a philosophy forum, it is a programming forum.

And I'm sorry but why and what for I need references it is entirely my business not yours and if you don't know the proper answer just don't give any answer (especially not asking another question) and stay quiet.

In that case don't ask question if you don't want us to help you.
When you post something, people ask question on why you are even doing something that looks like a bad design and it is.
Ok, from now on I won't help you. Others possibly wont either if you keep this you prick.

And P.S. Your code looks like a pile of crap, no offense.

Whats up with the attitude? I tried to help, but instead you insist that you are an asshole.

Its a pointer to a int, in which the int is a reference to another variable, thus it follows that p is a pointer to a (int)refrence


How about you fix your grammar before you ask question.


Again, are you talking English, or are you still in middle school ?

I did not say reference is the same as pointer, I said a pointer is sort of like a reference where when you change the reference value or a pointer value, the original value changes.

int* &a; //reads as a pointer that is a reference //Absurd
int& *b;  //read as a reference that is a pointer  ILLEGAL

why would you even want a pointer that is a reference, there should be no reason at all. You can achieve what you desire else wise.


Again you write what you don't mean.


Go F yourself. If you can't ask question correctly, then don't.

No if something get complicated you should try to find a simpler solution, than writing absurd code. This isn't a philosophy forum, it is a programming forum.

In that case don't ask question if you don't want us to help you.
When you post something, people ask question on why you are even doing something that looks like a bad design and it is.
Ok, from now on I won't help you. Others possibly wont either if you keep this you prick.

And P.S. Your code looks like a pile of crap, no offense.

Someone with very little knowledge like you shouldn't try to help in more advanced questions in the first place. Go to school, read more books, learn and then try but just try to help. And if you not sure say it. Otherwise you just look like an idiot. Big idiot with small knowledge.
Just like I said - your coding style tells that you know very little of c++ and that you are prick. Big stupid prick.
And POINTERS TO REFERENCES ARE ILLEGAL IN C++.
Learn how to read c++ code you fucking moron.

Edited 7 Years Ago by atch: n/a


And P.S. Your code looks like a pile of crap, no offense.

What code you fucking moron? Are you talking about this few lines that I've typed to have something to reffer to? If you can't see it that you are even more of an idiot than I've thought you are. Fucking moron.

This article has been dead for over six months. Start a new discussion instead.