Похожие чаты

Hi there, Do you have any problem copying class ? ISomething& alias

= getSomething()

CSomething *m_pSomthingImpl;
ISomething& getSomething()
{
return *m_pSomethingImpl;
}

if CSomethingImpl has some member variable,
the copy is not successfull leave _vptr with 0.
I though reference & is just an alias.
There is no need to copy data.
CMIIW.

getSomething().dismiss() <- it works.
but alias.dismiss() <- it doesnt.

9 ответов

22 просмотра

Works for me without issues, can you provide the code that works unexpectedly? https://wandbox.org/permlink/dxz05G7Q3hgGmwgm

John- Автор вопроса
Pavel
Works for me without issues, can you provide the c...

Because address of new object, luckyly the same. Therefore, Reference should not be used. *Alias is not set to another object yet. Sorry, just replied.

John
Because address of new object, luckyly the same. T...

Do you have a code example where it breaks? To understand what you mean

John- Автор вопроса
Pavel
Do you have a code example where it breaks? To und...

ISomething& temporary = getSomething(case1); temporary = getSomething(case2); temporary.dismiss() There should be error on case2 temporary.dismiss(). But it is ok if just : getSomething(case1).dismiss(); getSomething(case2).dismiss();

John
ISomething& temporary = getSomething(case1); tempo...

We couldn't compile this, give us a complete program including the problem.

John- Автор вопроса
John
Thanks Sir. Been solved.

Could you say the solution? i really don't understand what is your problem with the references: https://t.me/c/1071392087/169968

John- Автор вопроса
Ghasem Ramezani
Could you say the solution? i really don't underst...

Once initialized,reference cannot be changed to another object. https://en.cppreference.com/w/cpp/language/reference_initialization

Похожие вопросы

Обсуждают сегодня

Карта сайта