Topic: const constructors (Was: C++ seems to allow an implicit const-->non-const conversion under certain circumstances)


Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Fri, 1 Sep 2006 06:23:34 GMT
Raw View
Christoph Schulz posted:

> What I want to illustrate is that anyone who writes a constructor which
> saves "this" somewhere must think of the possibility that the object
> under construction is const. I don't think that C++ supports the
> programmer in such a case in any way.


Not sure if this is valid but how about:

class MyClass {
protected:

    bool IsConst() { return false; }
    bool IsConst() const { return true; }

public:

    MyClass()
    {
        if(IsConst())
        {

        }
        else
        {

        }
    }
};

or perhaps instead of:

class MyClass {
protected:

    char *p;
};

have:

class MyClass {
protected:

    char *p() { static char *p; return p; }

    char const *p() const { static char const *p; return p; }
};

--

Frederick Gotham

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "kanze" <kanze@gabi-soft.fr>
Date: Fri, 1 Sep 2006 09:56:15 CST
Raw View
Frederick Gotham wrote:
> Christoph Schulz posted:

> > What I want to illustrate is that anyone who writes a
> > constructor which saves "this" somewhere must think of the
> > possibility that the object under construction is const. I
> > don't think that C++ supports the programmer in such a case
> > in any way.

> Not sure if this is valid but how about:

> class MyClass {
> protected:

>     bool IsConst() { return false; }
>     bool IsConst() const { return true; }

> public:

>     MyClass()
>     {
>         if(IsConst())

Always returns false.

--
James Kanze                                           GABI Software
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: fgothamNO@SPAM.com (Frederick Gotham)
Date: Fri, 1 Sep 2006 16:08:24 GMT
Raw View
Frederick Gotham posted:


>     char *p() { static char *p; return p; }
>
>     char const *p() const { static char const *p; return p; }


I made a novice mistake with the static objects there... I realised 5 seconds
after I posted but it was too late!

--

Frederick Gotham

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: giecrilj@stegny.2a.pl ("Kri tof elechovski")
Date: Sat, 2 Sep 2006 15:24:38 GMT
Raw View
Uzytkownik "Ron Natalie" <ron@spamcop.net> napisal w wiadomosci
news:44f75521$0$24214$9a6e19ea@news.newshosting.com...
> Christoph Schulz wrote:
>
>> To make old code work, one could imagine a deprecated "T const*" => "T*"
>> conversion within const constructors (like for string literals which can
>> be converted to char*).
>>
>>
> No, I do not believe you can.
>
> struct C {
> C() {
>     foo(this);
> }
> void foo();
> void foo() const;
> };
>
> Which one would your "fix" execute?
>

Neither of the above.
Chris


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]