ABC question

This is a discussion on ABC question within the C++ Programming forums, part of the General Programming Boards category; If I have an ABC with a non-default, non-virtual constructor that has an implementation and 2 virtual functions, 1 pure ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    119

    ABC question

    If I have an ABC with a non-default, non-virtual constructor that has an implementation and 2 virtual functions, 1 pure virtual, and 1 not pure virtual with no implementation and I have code like:

    Code:
    MyABC *foo[10];
    when I compile and link, during linking there is a vtable symbol error.

    If I comment out the non-default, non-virtual constructor's implementation, it links. Why would it link? Shouldn't it still complain about the non-pure virtual function with no implementation?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post the smallest example program that demonstrates the issue.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    Quote Originally Posted by oogabooga View Post
    Post the smallest example program that demonstrates the issue.
    It's so simple, didn't think I needed one.

    Code:
    class hello
    {
    public:
    	hello(int x);
    	
    	virtual void hello1() = 0;
    	
    	virtual void hello2();
    	
    	
    private:
    	int val;
    };
    
    hello::hello(int x) : val(x)
    {
    	
    }
    
    int main(void)
    {
    	hello *myhello[10];
    	
    	return 0;
    }
    If you comment out the constructor implementation. It links. Or if you assign 0 to hello2, it links.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Strange... the error does not appear if the constructor is defined inline.

    I think gcc gets confused about something but whether it is a bug... or a well defined feature.. I have no idea.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's so simple, didn't think I needed one.
    Indeed. But code is more trustworthy than an explanation in words. And why should I have to code up your example myself, especially since you may have left something out of your explanation? ALWAYS post a code example.

    For instance, it also links if you define the ctor inline, which you did not mention.

    Still, I don't know what's going on here....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    119
    hello2 should be set to 0, though, correct? Or does it not actually matter?

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by homer_3 View Post
    Or does it not actually matter?
    Somewhat correct... imo.
    The only difference is that different errors will be shown if any of the derived classes do not define it.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    Well, I notice that you never actually create any objects of the class, so all you needed was a forward declaration of the class.
    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

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,166
    The output of some compilers, when given an ABC with a non-pure virtual function declared but not defined, does confuse some linkers.

    In your case, define (i.e. implement) hello::hello2(). It doesn't matter if its definition is inlined within the class definition or not.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 03:59 AM
  3. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  4. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 01:02 PM
  5. A question of color...(not a racial question)
    By Sebastiani in forum Windows Programming
    Replies: 7
    Last Post: 01-15-2003, 07:05 PM

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