Class pointers

This is a discussion on Class pointers within the C++ Programming forums, part of the General Programming Boards category; Okay, I am making a program for my brother, but I can seem to handle the objects like I want. ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Class pointers

    Okay, I am making a program for my brother, but I can seem to handle the objects like I want. So, I was wondering if any of you guys/girls could assist me. This is what I have so far, I have highlighted the problem in red, and the errors are at the bottom of post thank you for any assistance.
    Code:
    #include <iostream>
    #include <windows.h>
    #include <mmsystem.h>
    #include <string>
    #include <deque>
    #include <map>
    
    using namespace std;
    class file_i {
        public:
        file_i();
        ~file_i();
    
        virtual void setupF() const = 0;
        private:
        string files[20];
    };
    
    file_i::file_i() {
        //
    };
    
    file_i::~file_i() {
        //
    };
    
    class player : private file_i
    {
        public:
        player();
        ~player();
    
        virtual void setupF() const {
            //
        }
    
        private:
        deque<string> names;
        deque<int> s_num;
        map<int, string> manage;
    };
    
    player::player() {
        file_i* p_one = new player;  //this works but is the cause of error, with //delete
    };
    
    player::~player() {
        delete p_one;   this is line 48
    };
    
    
    int main()
    {
    
    }
    this is the error
    Code:
    error: 'p_one' undeclared ( first use of this function ) Line 48
    yet again thank you for assistance

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You are deriving privately from file_i, so you don't need to allocate space for a file_i object. There is already space in the player object for file_i data.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    okay, thanks but yet again how do I fix my error? I will change the code,but I still have no idea how to handle the object for player. managing inside of main would be okay, but I am going to use multiple functions so it wouldnt work right? to say this is a music player, and I had 5 functions ( play, stop, pause, restart, and exit ). How would I create, and delete an object that can be used by all of these. Thank you

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> how do I fix my error?
    Remove the code in the constructor and destructor for player.

    >> I still have no idea how to handle the object for player.
    What do you mean? A player is a class like any other. You use it like you would use any other.

    What specifically is confusing you? Do you understand private inheritance and why you are using it here?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Actually, no I don't know why i'm using private inheritance, it is just what I start learning with so I stuck with it. Usually with my classes I create an instance of an object inside of main, but in the situation I can't because, if I tried to use it in any other function I would get one of 2 errors one is multiple definitions, or first use of function. I just do not understand how to fix this. Thanks for any assistance

    EDIT: would this work?
    Code:
    int main() {
      static player* p_one = new player;
    }
    Last edited by Raigne; 01-03-2007 at 02:30 PM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Hm. Why exactly are you creating a new player object inside the constructor of the player object? If that worked, you'd end up with an infinitely repeating constructor; every newly created player would again create a new player.

    The only thing you have to do in a constructor is initialise the member variables, in this case names, s_num and manage. AFAIK the fact that you're privately inheriting doesn't change anything about that.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Okay, here is updated code. This is correct right?
    Code:
    #include <iostream>
    #include <windows.h>
    #include <mmsystem.h>
    #include <string>
    #include <deque>
    #include <map>
    
    using namespace std;
    class file_i {
        public:
        file_i();
        ~file_i();
    
        virtual void setupF() const = 0;
        private:
        string files[20];
    };
    
    file_i::file_i() {
        //
    };
    
    file_i::~file_i() {
        //
    };
    
    class player : private file_i
    {
        public:
        player();
        ~player();
    
        virtual void setupF() const {
            //
        }
    
        private:
        deque<string> names;
        deque<int> s_num;
        map<int, string> manage;
    };
    
    player::player() {
    
    };
    
    player::~player() {
        names.clear();
        s_num.clear();
        manage.clear();
    };
    
    
    int main()
    {
        static player* p_one = new player;
    }
    Last edited by Raigne; 01-03-2007 at 02:36 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    void my_func(const player& p1) { }
    
    int main()
    {
      player p_one;
      my_func(p_one);
    }
    It seems you are using advanced language features when basic ones will do.

    >> This is correct right?
    No. There is no need to make names, s_num and manage pointers. Leave them as objects.

    You don't need to use new or delete very often in C++, so if you are using them, question whether you can use a simple object instead.
    Last edited by Daved; 01-03-2007 at 03:06 PM.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Ok, sorry for questions, one more problem, that code you gave I get the following error.
    Code:
    'p1' undeclared (first use of this function
    I probably missed something

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That was a typo. I should have written my_func(p_one).

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Ah yes I should have seen that sorry, I am eternally grateful for the fast replies and helpful answers. One last thing tho what is the difference in private, protected,and public inheritance. Is the just a access specifier for the child class, or something else?Thanks
    EDIT: ..im not doing this right
    Code:
    //classes are up top no difference
    void my_func(const player& p1) { }
    
    int main() {
       player p_one;
       my_func(p_one);
    }
    
    int myfunction() {
       p_one->setupF();
    }
    EDIT 2: I tried sumthing is this right?

    Code:
    void my_func(const player& p1) { }
    
    int myfunction();
    int main()
    {
        player p_one;
        my_func(p_one);
    }
    
    int myfunction(const player p_one) {
    }
    Last edited by Raigne; 01-03-2007 at 03:23 PM.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    They are very different things. Private inheritance models "implemented-in-terms-of". In other words, when you are using private inheritance, you are saying that the current class uses the parent class for part of its implementation. Another way to model "implemented-in-terms-of" is to use simple member variables (composition).

    For example, your player class is implemented in terms of the file_i class, as well as two deques and a map. You would choose private inheritance over composition only for a few specific reasons. One reason would be if you wanted to override some virtual functions in the parent class. There are other reasons, but it is somewhat rare to use private inheritance. You might not even need it in this case.

    Protected inheritance is extremely rare. There might be some uses for it, but generally you can just assume it doesn't exist.

    Public inheritance models "is-a" or "works-like-a". In other words, the derived object can be used as a parent class object in code that uses the parent class pointer or reference. The parent class has an interface, and the derived class implements that interface in its own way so that it can be used wherever the interface is used.

    The point is that even though these seem similar, they model very different things and should generally be used in very different situations if you use them at all.

  13. #13
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Code:
    void my_func(player& any_name);
    int myfunction(player& some_name);
    
    int main()
    {
        player p_one;
        //use these functions to mess with player one
        my_func(p_one);
        myfunction(p_one);
        return 0;
    }
    
    int myfunction(player & any_name) {
    //operate on any_name directly to modify p_one
    }
    
    void my_func(player& some_name) {
    //operate on some_name directly to modify p_one
    }
    Last edited by CodeMonkey; 01-03-2007 at 03:31 PM. Reason: return 0;
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I just cannot get this to work, I have been trying and trying for over 3 hours now, and it isnt working. I have searched google, asked here, and still.... I am losing faith on this project. Sad thing is ive done harder things than this, but this I cant seem to grasp at all

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Look at CodeMonkey's example. Just fill in the code in the function.

    This is actually just basic function calling and variable passing that you should have done many times before.
    Last edited by Daved; 01-03-2007 at 03:43 PM.

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

Similar Threads

  1. storing pointers to a templated class in a vector
    By rt454 in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2009, 02:04 AM
  2. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 01:10 PM
  3. Replies: 3
    Last Post: 10-31-2005, 11:05 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

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