简体   繁体   中英

int *const a = int const *b , is that true for obj ( obj *const a = obj const *b )?

in this question they said that

What is the difference between const int*, const int * const, and int const *?

const int * == int const *

OK this true

but when i use it in objects (in my case direct3ddevice and GUI Objects from CEGUI Library )

look at this

void GUI_Menu::Add_Popup( MenuItem const *Parent ,const String &Name )
{
    PopMenu.resize (Popup_Menu_ID+1 );
    PopMenu.at (Popup_Menu_ID) = static_cast <PopupMenu*>  (CEGUI::WindowManager::getSingletonPtr()->createWindow ("TaharezLook/PopupMenu" , Name));

    Parent->addChildWindow (PopMenu.at (Popup_Menu_ID)); 
    Popup_Menu_ID++ ;
} 

this code will compile error in the following line

Parent->addChildWindow (PopMenu.at (Popup_Menu_ID));

i will explain why it's wrong (in my point) and tell is this false or true;

MenuItem is a class contain data and function, and those data must updated in needed

as example ( settext, color, size....etc );

now if i create new MenuItem Obj like this;

MenuItem const *Obj

what is mean that...OK

this mean that we can't change the data inside the obj but we can change the address
in other world

value is constant but the address is not so, if we want to change color, size...etc we can't and we get error...............is this true;

in other hand we have the same code but with change the constant according.

void GUI_Menu::Add_Popup( MenuItem *const Parent ,const String &Name )
{
    PopMenu.resize (Popup_Menu_ID+1 );
    PopMenu.at (Popup_Menu_ID) = static_cast <PopupMenu*> (CEGUI::WindowManager::getSingletonPtr()->createWindow ("TaharezLook/PopupMenu" , Name));

    Parent->addChildWindow (PopMenu.at (Popup_Menu_ID)); 
    Popup_Menu_ID++ ;
}

we change the following line

MenuItem *const Parent

the new line is mean that we can change value but no change the address so

 obj *const a = obj const *b 

is wrong !!!

Your initial premise is incorrect. int *const a is not the same type as int const *b . The first is a constant pointer to a non-constant int. The second is a non-constant pointer to a constant int.

If the const keyword is to the left of the pointer symbol, then they are equivalent:

const int* pA == int const* pB

If const is on the right it's a different thing. In the first case, the data is constant while the pointer is not, in the latter is the opposite: the pointer is constant while the data is not.

Take a look at this tutorial , it should clarify you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM