|
Prev: Unable to initialize COM in VC Dll when invoked via C# application
Next: How can I make a connection in two TreeCtrls.
From: lost_in_space on 19 May 2008 09:49 Hey, all - I have a member method of a class that returns a pointer to an internal object. I want clients of the class to be unable to modify the internal object pointed at, so I precede the return type with 'const'; e.g., 'const InnerObject* GetInnerObject();'. I also want this method tagged as 'const' to indicate that it is a constant method that does not modify the outer object, so variables of the outer type, if declared to be 'const' themselves, will be able to access the method. Typically this would entail adding the 'const' keyword after the parameter list for the method; e.g., 'InnerObject* GetInnerObject() const;'. So if I want both; that is, I want both the returned pointer to be 'const', and I want the method itself recognized as 'const', would I need to include 'const' twice in the same declaration/definition? That is, would 'const InnerObject* GetInnerObject() const;' be correct? It seems like it would be, but it just looks weird with two 'const' keywords in the same declaration. TIA... : )
From: Giovanni Dicanio on 19 May 2008 10:07 "lost_in_space" <a(a)b.com> ha scritto nel messaggio news:u$rq4cbuIHA.420(a)TK2MSFTNGP02.phx.gbl... > would I need to include 'const' twice in the same declaration/definition? > That is, would 'const InnerObject* GetInnerObject() const;' be correct? Yes, it is correct. > It seems like it would be, but it just looks weird with two 'const' > keywords in the same declaration. It is not weired. In fact, you can have a const method that returns a non-const object, e.g. SomeObject * SomeFactory::BuildSomeObject() const { ... return new SomeObject(...); } The caller can modify SomeObject instance (returned by pointer); but the BuildSomeObject method (of class SomeFactory) does not modify the internal status of SomeFactory instance. Giovanni
From: SvenC on 19 May 2008 10:08 Hi lost_in_space, > I have a member method of a class that returns a pointer to an > internal object. I want clients of the class to be unable to modify > the internal object pointed at, so I precede the return type with > 'const'; e.g., 'const InnerObject* GetInnerObject();'. > > I also want this method tagged as 'const' to indicate that it is a .... > So if I want both; ... > would 'const InnerObject* GetInnerObject() const;' be correct? Yes, that's the correct syntax. -- SvenC
From: lost_in_space on 19 May 2008 10:54 Got it. ; ) "Giovanni Dicanio" <giovanni.dicanio(a)invalid.com> wrote in message news:eAhXknbuIHA.5580(a)TK2MSFTNGP04.phx.gbl... > > "lost_in_space" <a(a)b.com> ha scritto nel messaggio > news:u$rq4cbuIHA.420(a)TK2MSFTNGP02.phx.gbl... > >> would I need to include 'const' twice in the same declaration/definition? >> That is, would 'const InnerObject* GetInnerObject() const;' be correct? > > Yes, it is correct. > >> It seems like it would be, but it just looks weird with two 'const' >> keywords in the same declaration. > > It is not weired. > In fact, you can have a const method that returns a non-const object, e.g. > > SomeObject * SomeFactory::BuildSomeObject() const > { > ... > return new SomeObject(...); > } > > The caller can modify SomeObject instance (returned by pointer); but the > BuildSomeObject method (of class SomeFactory) does not modify the internal > status of SomeFactory instance. > > Giovanni > >
From: lost_in_space on 19 May 2008 10:54
Thanks , Sven. "SvenC" <SvenC(a)nospam.nospam> wrote in message news:BB722AB5-B489-4A86-B97A-CF8E4F4068C3(a)microsoft.com... > Hi lost_in_space, > >> I have a member method of a class that returns a pointer to an >> internal object. I want clients of the class to be unable to modify >> the internal object pointed at, so I precede the return type with >> 'const'; e.g., 'const InnerObject* GetInnerObject();'. >> >> I also want this method tagged as 'const' to indicate that it is a > ... >> So if I want both; ... >> would 'const InnerObject* GetInnerObject() const;' be correct? > > Yes, that's the correct syntax. > > -- > SvenC |