Question about private nested classes/structs

This is a discussion on Question about private nested classes/structs within the C++ Programming forums, part of the General Programming Boards category; Hello, can someone please tell me, why it's possible to access the member of a returned private nested struct? Shouldn't ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Austria
    Posts
    4

    Question about private nested classes/structs

    Hello,

    can someone please tell me, why it's possible to access the member of a returned private nested struct? Shouldn't the compiler complain about the privateness of the returned data type?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class TestClass
    {
    private:
        struct TestStruct
        {
            int A, B, C;
        };
    
    public:
        TestStruct get()
        {
            TestStruct t = { 1, 2, 3 };
            return t;
        }
    };
    
    int main( int argc, char **argv )
    {
        TestClass *t = new TestClass();
    
        // Here the compiler mourns about privateness - as expected
        TestClass::TestStruct e = t->get();
    
        // This one works, but why?
        int a = t->get().A;
    }
    Thanks,
    egrath

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The type itself is private but the members are public.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    By having a public method return a TestStruct you effectively negate the privateness of it. If you want TestStruct to be private then you shouldn't return any TestStruct through public methods.

    The problem really becomes apparent with C++0x's type inference. You can infer the returned TestStruct type and then start instantiating as many TestStructs as you like outside of TestClass.
    Last edited by BMJ; 10-22-2010 at 02:12 PM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    Austria
    Posts
    4
    Hello,
    thanks you for bringing light to this issue!
    egrath

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Why are you creating a new instance via new?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Location
    Austria
    Posts
    4
    Hello Elysia,

    what's wrong with creating a new instance via new?

    egrath

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    What is the point? Do you need it? Are you aware of the overhead? Are you aware that you're creating a memory leak? Why aren't you wrapping the raw pointer in a smart pointer?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Location
    Austria
    Posts
    4
    Hello Elysia,

    the code i've posted when starting the thread is just an example because i wanted to add some code to the question for better understanding - think of it as working pseudo code which has nothing to do with real life code and good coding practices.

    egrath

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Of course. I just want to make sure you don't do anything silly in your real code.
    As long as you understand the goods and bads of it, then it's fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question from a newbie regarding nested function
    By Sharifhs in forum C Programming
    Replies: 19
    Last Post: 08-10-2010, 07:44 AM
  2. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 04:23 PM
  3. Currency Calculator
    By desiamerican in forum C# Programming
    Replies: 1
    Last Post: 01-21-2010, 09:53 PM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 04:44 PM
  5. The Private game 2
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-04-2002, 03:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21