non-pure virtual function in C++ interface

This is a discussion on non-pure virtual function in C++ interface within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Seems when using __interface to define a C++ interface in Visual Studio, we can provide non-pure virtual function. ...

  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
    22,157
    According to MSDN, a Visual C++ interface "can only contain public, pure virtual methods".
    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
    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,893
    __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,893
    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,893
    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,893
    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, 04:45 AM
  2. Pure virtual function error
    By pandu in forum C++ Programming
    Replies: 9
    Last Post: 06-10-2008, 05:21 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Information Regarding Pure Virtual Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2003, 04:43 AM

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