Problem with pointers to dynamic objects

This is a discussion on Problem with pointers to dynamic objects within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by CornedBee Public and private inheritance share pretty much nothing, conceptually. Public inheritance is a subtype relationship requiring ...

  1. #16
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by CornedBee View Post
    Public and private inheritance share pretty much nothing, conceptually. Public inheritance is a subtype relationship requiring substitutability. To put it in terms of Coplien, it's a form of positive variance. Public bases are part of the interface of a class.
    Private inheritance is strictly an implementation detail. It is absolutely no business of any other code that a class privately inherits something. Private inheritance is merely a convenience over containment and implementation in terms of a single subobject.
    Sorry if this sounds kind of dumb, but are you saying that if I use private inheritance then the child class will not inherit any of its parents variables?

    Also , I'm still having problems with these pointers . Once again the notes I got don't cover dynamic objects created within objects. I guess maybe I need a book :P

    I'm trying to get the pointer to the guest staying in a room. Heres my get member function:
    Code:
    guests rooms::GetGuest(){return *p;}
    And heres where I call it:
    Code:
    void CheckAvail(rooms *room)
    {  
        for(int i=1; i<=num_rooms; i++)
        {
            cout << "Room: " << room[i].GetNumber() << " \tat $" << room[i].GetCost() << "\t";         
            if(room[i].GetGuest() != NULL)
                cout << "Booked\n";
            else
                cout << "Vacant\n";
        }
    }
    When I run the prog I get this error at the IF statement:
    Code:
    no match for 'operator!=' in 'rooms::GetGuest()() != 0'
    Could someone please point out whats wrong here. Thx.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by mike_g View Post
    Sorry if this sounds kind of dumb, but are you saying that if I use private inheritance then the child class will not inherit any of its parents variables?
    No, it will inherit the parent's members. But they won't be visible to the outside. For all practical purposes, there are only three differences between B and C:
    Code:
    class A {};
    class B : private A {};
    class C {
      A a;
    };
    Difference 1 is the syntax to access the A members. Class B can use them without qualification, class C has to access them through a. Also, for exposing selected members of A, class C has to write a forwarding function, while class B merely needs a using declaration.
    Difference 2 is that class B has access to A's protected members, while class C does not.
    Difference 3 is that the empty base class optimization can be applied in the case of class B, but not in the case of class C. (There may be an empty subobject optimization, but I'm not sure about that. I don't really think so.)

    I guess maybe I need a book :P
    Sounds like an excellent idea. This assignment gives me huge doubts about the quality of the class you're taking.
    Last edited by CornedBee; 04-25-2007 at 12:13 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    GetGuest dereferences the pointer, yet the calling code acts like it returned the pointer directly.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Cool, I chucked an ampersand in the start of the if statement and that seems to have fixed it. Thanks man

  5. #20
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Hmm... I spoke too soon :/ The program now compiles but when it gets to the IF statement it crashes.

    Heres the function again:
    Code:
    guests rooms::GetGuest(){return *p;}
    And heres my new dodgy IF statement:
    Code:
    if(&room[i].GetGuest() != NULL)
    Compiler message: taking address of temporary

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This effect is called slicing. Don't do it.

    Rather, just return a pointer from GetGuest().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  2. Problem with Vectors And Pointers
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 01-21-2006, 06:23 PM
  3. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  4. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  5. problem , need pointers. thanks
    By mobius in forum C Programming
    Replies: 4
    Last Post: 09-03-2001, 05:12 AM

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