6 Replies - 6793 Views - Last Post: 19 May 2010 - 11:58 AM Rate Topic: -----

#1 gda2004  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

What does & mean after a variable ?

Posted 19 May 2010 - 04:36 AM

Hi there I a wondering what & means after a variable for example const Matrix44 &

w = new Matrix44( &xm);

here is a prototype of the object I am trying to create Matrix44(){ identity(); } .This code is from my lecturer and I was just wondering if anyone could tell me what I am supposed to put into it I have tried a few things can someone help me plz ???

this is the error I am having
error C2664: 'Matrix44::Matrix44(const Matrix44 &)' : cannot convert parameter 1 from 'const Matrix44 *' to 'const Matrix44 &'

Is This A Good Question/Topic? 0
  • +

Replies To: What does & mean after a variable ?

#2 Krishs  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 105
  • Joined: 06-April 10

Re: What does & mean after a variable ?

Posted 19 May 2010 - 05:03 AM

The Matrix44 constructor is supposed to take reference of anather Matrix44
not a pointer.

Matrix44::Matrix44(const Matrix44 &)


so just pass it as it is no need to & (Address of) operator

w = new Matrix44(xm);

This post has been edited by Krishs: 19 May 2010 - 05:08 AM

Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: What does & mean after a variable ?

Posted 19 May 2010 - 08:03 AM

Quote

The Matrix44 constructor is supposed to take reference of anather Matrix44
not a pointer.


Passing by reference is pretty much the same thing as passing using a pointer, there both passing memory locations
Was This Post Helpful? 1
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: What does & mean after a variable ?

Posted 19 May 2010 - 08:27 AM

When you declare a variable as:

int &var = var2; -- you are creating a refernce of var2, an alias for var2
int *ptr = &var; -- here & is the "address of" operator and is returning he "address of" var.
void swap(int &a, int& b ); -- here I am declaring a function that takes two arguments "by reference" -- note that the variable type is "int &" and the & is associated with the type and not the variable name. That is why some people prefer to do this as "int& b" so that & is with the int and not the variable name. Others see the "int" type to be more important and the & to just mean "reference to".

The '&' never comes AFTER a variable name unless it is the bitwise AND operator.

int a = 1;
int b = 3;
int c = a & b;

Was This Post Helpful? 0
  • +
  • -

#5 gda2004  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: What does & mean after a variable ?

Posted 19 May 2010 - 09:31 AM

sorry guys I got confused because it was directly after the variable it just slipt my mind about & are the same as points with respect to touching either the name or the variable type
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: What does & mean after a variable ?

Posted 19 May 2010 - 10:10 AM

:) It was not directly after the variable! That does not exist is C++, it was directly after the TYPE.

const Matrix44 & <--- a TYPE name (the whole thing... it is a "reference to a Matrix44 that is constant")

For some strange reason many programmers do not put variable names in function signatures (although with modern IDE's it really is better to give the variable name in the declaration because it helps document the function for "intelisense" features). So what you were seeing was a type from a function definition.

The & does NOT go after a variable name (unless you are talking about the bitwise and operator).
Was This Post Helpful? 0
  • +
  • -

#7 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: What does & mean after a variable ?

Posted 19 May 2010 - 11:58 AM

Using an ampersand after a variable could only be viewed as a bitwise operation (i.e Logical AND)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1