Thread: pointers for class

  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
    28,413
    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'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM