Thread: non-pure virtual function in C++ interface

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    non-pure virtual function in C++ interface

    Hello everyone,


    Seems when using __interface to define a C++ interface in Visual Studio, we can provide non-pure virtual function. Previously I think in interface, only pure virtual function is allowed. Seems there is no such restriction now.

    Correct?


    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    According to MSDN, a Visual C++ interface "can only contain public, pure virtual methods".
    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
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Yes, laserlight. I have read thay link before asking this question, but it seems the code can compile in Visual Studio 2008. Looks strange.

    BTW: in COM, the interface should be struct, not __interface, right?

    Quote Originally Posted by laserlight View Post
    According to MSDN, a Visual C++ interface "can only contain public, pure virtual methods".

    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    __interface is just an alias for struct with a __declspec(novtable) attribute attached.

    Therefore, whatever the documentation might say about what an interface may contain, this is not actually enforced by the compiler. It's just part of the COM rules: if you interface contains anything besides public, pure virtual functions, you violate these rules and interoperability is no longer guaranteed.
    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

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    __interface is just an alias for struct with a __declspec(novtable) attribute attached.

    Therefore, whatever the documentation might say about what an interface may contain, this is not actually enforced by the compiler. It's just part of the COM rules: if you interface contains anything besides public, pure virtual functions, you violate these rules and interoperability is no longer guaranteed.
    I have got your point. Your suggestion is trying best not to violate the rule and put non-virtual function into __interface, right? :-)


    regards,
    George

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes.

    Well, actually I'd simply not use __interface, but that's a different issue.
    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

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee!


    What is it -- "that's a different issue"? :-)

    Quote Originally Posted by CornedBee View Post
    Yes.

    Well, actually I'd simply not use __interface, but that's a different issue.

    regards,
    George

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Means even when I'm using MS's COM, doesn't mean I'm going to use MS's language extension.
    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

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    If you are using COM, how could you avoid using interface -- as C++ extension?

    Quote Originally Posted by CornedBee View Post
    Means even when I'm using MS's COM, doesn't mean I'm going to use MS's language extension.

    regards,
    George

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    By using class or struct. Duh.
    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

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    My question is answered.

    Quote Originally Posted by CornedBee View Post
    By using class or struct. Duh.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Pure virtual function error
    By pandu in forum C++ Programming
    Replies: 9
    Last Post: 06-10-2008, 04:21 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Information Regarding Pure Virtual Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2003, 04:43 AM