Y
Y
Yu Yu2018-08-07 16:03:14
C++ / C#
Yu Yu, 2018-08-07 16:03:14

How to get a reference to an object?

Hello!
I can not fully understand the work with passing objects by reference

Class A
{
  B *_b;
  C _c2;
  C *_c;
  
  A(){
    B _b = new B();
    C _c = new C();
    
    _b->setC(*_c);
    _b->setC(_c2);
  }
}

Class B
{
  C *_c;
  
  void setC(const C& c){
    _c = c;
  }
}

Class C
{
  
}

As far as I understand, in a function with parameters (const & C c), if the object was created by the new operator, then the pointer value is passed, and if statically, then the link.
Inside the function, is it just a number - the memory address where the object is located?
How can I now assign the received address to the C * c pointer inside the _b class?
ошибка: cannot convert 'const C' to 'C*' in assignment

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-08-07
@res2001

Correctly:
No - a link is passed anyway. If you have a pointer and you want to pass a reference, then you must dereference the pointer.
Better not to bother with this issue. There are no references at the assembler level, so in fact the references are the same veiled pointers that have limited functionality at the compiler level for safer use.
The pointer is an integer. The size of a pointer does not depend on the type it points to, it depends only on the platform (in x64 pointers are 64 bits, and in x32 - 32 bits).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question