pointers for class

This is a discussion on pointers for class within the C++ Programming forums, part of the General Programming Boards category; I have something like this Code: class pub { public : void print(){cout<<-----<<endl;} }; class man:public pub { public: void ...

  1. #1
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54

    pointers for class

    I have something like this
    Code:
    class pub
    {
    public  :
    void print(){cout<<-----<<endl;}
    };
    class man:public pub
    {
     public:
    void print(){cout<<"----------------------"<<endl;}
     };
    int main()
    {
    pub *p1;
    p1=new man;
    pub->print();
    return 0 ;
    }
    When i try to call the print function from class man I get the print function from class pub..
    How can i fix this..
    I cant make print virtual and I must use pub pointer I try with reintrepret_cast but it doesnt work

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    I cant make print virtual and I must use pub pointer I try with reintrepret_cast but it doesnt work
    Making print virtual is the correct approach. If you cannot change the base class, then it implies that print was not meant to be overriden in the first place, so what is ask for is not only (pretty much) impossible, but also 'immoral'.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    Sorry i didnt understand the question of my professor i can make print virtual but thanks for the answer laserlight...I was wondering why reinterpret_cast didnt work..
    when i write this part of code
    Code:
    reinterpret_cast<man>(p1);
    this should reinterpret p1 and make it man pointer or not?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    this should reinterpret p1 and make it man pointer or not?
    No, that interprets it as a man object, not pointer. What you are looking for is reinterpret_cast<man*>, though better would be static_cast<man*>. In that respect I was probably wrong to call it "(pretty much) impossible", though it is definitely not the correct approach.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    No, that interprets it as a man object, not pointer. What you are looking for is reinterpret_cast<man*>, though better would be static_cast<man*>.
    Yes the compiler let me do this but the result is that is called the print function from class pub,I tried and static_cast and I get the same result...
    Why it doesnt work?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Yes the compiler let me do this but the result is that is called the print function from class pub,I tried and static_cast and I get the same result...
    You can try:
    Code:
    #include <iostream>
    
    class pub
    {
    public:
        void print()
        {
            std::cout << "pub::print()" << std::endl;
        }
    };
    
    class man : public pub
    {
    public:
        void print()
        {
            std::cout << "man::print()" << std::endl;
        }
    };
    
    int main()
    {
        pub* p1 = new man;
        static_cast<man*>(p1)->print();
        delete p1;
        return 0;
    }
    But of course the better solution is:
    Code:
    #include <iostream>
    
    class pub
    {
    public:
        virtual void print()
        {
            std::cout << "pub::print()" << std::endl;
        }
    };
    
    class man : public pub
    {
    public:
        void print()
        {
            std::cout << "man::print()" << std::endl;
        }
    };
    
    int main()
    {
        pub* p1 = new man;
        p1->print();
        delete p1;
        return 0;
    }
    You might consider making print() const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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