Topic: New casting operator


Author: nobody@nowhere.com ("Anonymous")
Date: Mon, 24 Feb 2003 19:55:58 +0000 (UTC)
Raw View
C++ should have a casting operator, private_cast:

class C
{
private:
    int x;
public:
    int y;
};

int main()
{
    C c;
    (private_cast<C public*>(&c))->x = 3;

    C private& foo = c;
    foo->y = 3; //Compiler error
}

This would be helpful for debugging and also if you are using someone else's
API and they left something out, like an accessor.


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Mon, 24 Feb 2003 21:20:28 +0000 (UTC)
Raw View
""Anonymous"" <nobody@nowhere.com> wrote in message news:71%5a.10305$ep5.8860@nwrddc02.gnilink.net...

>     (private_cast<C public*>(&c))->x = 3;
>     C private& foo = c;

First you're going have to explain what the words "public" and "private" mean when
associated with the type C.

> This would be helpful for debugging and also if you are using someone else's
> API and they left something out, like an accessor.

Debugging?   Looks like it would be helpful in generating a whole new set of problems.




---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: philippe_mori@hotmail.com ("Philippe Mori")
Date: Mon, 24 Feb 2003 23:15:37 +0000 (UTC)
Raw View
> C++ should have a casting operator, private_cast:
>
> class C
> {
> private:
>     int x;
> public:
>     int y;
> };
>
> int main()
> {
>     C c;
>     (private_cast<C public*>(&c))->x = 3;
>
>     C private& foo = c;
>     foo->y = 3; //Compiler error
> }
>
> This would be helpful for debugging and also if you are using someone
else's
> API and they left something out, like an accessor.
>
>

If you want more access for debugging, why not uses a friend helper class.

class C {
    friend class CPrivateAccessor;

private:
    int x;
public:
    int y;
};

class CPrivateAccessor {
public:
    CPrivateAccessor(C &) : c(c_) {}

    int &x() { return c.x; }

private:
    C &c;
};

int main() {
    C c;

    CPrivateAccessor debug_c(c);
    debug_c.x() = 3;

    return 0;
}


Or alternatively store all your private data in a struct and add
a public function to class C to get the private data;

class C {
public:
    struct PrivateData {
        int x
    };

    PrivateData &GetPrivateData() { return data; }

private:
    PrivateData data;
}

So no language extension needs for your purpose...


I do not understand the purpose of the private modifier
applied to foo in your example. I suppose that you want
to prevent change to foo but then what you can do with
it? You should not even be able to convert it back to a
C public & without doing a cast (to ensure that the type
system is safe).

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: llewelly.at@xmission.dot.com (LLeweLLyn)
Date: Mon, 24 Feb 2003 23:15:41 +0000 (UTC)
Raw View
nobody@nowhere.com ("Anonymous") writes:

> C++ should have a casting operator, private_cast:
>
> class C
> {
> private:
>     int x;
> public:
>     int y;
> };
>
> int main()
> {
>     C c;
>     (private_cast<C public*>(&c))->x = 3;
>
>     C private& foo = c;
>     foo->y = 3; //Compiler error
> }
>
> This would be helpful for debugging and also if you are using someone else's
> API and they left something out, like an accessor.
[snip]

I see you too have had some 3rd party library vendor reply to your
    complaint about a frustrating bug or deficiency with 'well, just call
    this private function ...', or 'just tweak that private member ...'.

Despite that, I dislike your suggestion; I don't think it enables any
    good constructs we can't currently express.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]