LouArnold

I'm a bit confused about the above keywords. The documentation doesn't expressly answer my questions:

1) Are these MS CLR specific or also included in the C++ standard

2) When do you use "interface" and when do you use "abstract"

I am familiar with their syntax, but its not clear which to use to declare a managed base class with a few pure virtual functions. The class is to be used as an interface class (as in Java), and will be inheritted by subclasses. The objective is to call functions of subclasses with an object pointer to their base class.
[code]
//----------File Iconfig.h
using namespace System;
ref class IConfig abstract
{
public:
IConfig(void){};

//"=0" makes pure virtual method that subclass MUST implement
virtual String ^Config()=0; //pure virtual
virtual bool Config(const String ^sConfig)=0;

};
//-------File IConfig.cpp
#include "StdAfx.h"
#include "IConfig.h"
//The following code won't compile:
//error: "cannot define an abstract method of a managed class"
String^ IConfig::Config() //pure virtual
{ return "No IConfig::Config Override!";
}
bool IConfig::Config(const String ^sConfig)
{
return false;
}
//subclass .h file
#include "IConfig.h"
using namespace System;
ref class FileRenameOptions: public IConfig
{
public:
//For configuration read and write
virtual String^ Config(); //returns string like "NoFileRename"
override bool Config(String^ sConfig); //returns true if option text is recognized.
....
}
[/code]
I guess I have the same questions for "override" "virtual", and "new" when applied to subclass functions (ie methods). These seem to go with "abstract". What goes with "interface", if anything





Re: Visual C++ Language C++ "interface" and "abstract" keywords

einaros

LouArnold wrote:
I'm a bit confused about the above keywords. The documentation doesn't expressly answer my questions:

1) Are these MS CLR specific or also included in the C++ standard

C++/CLI has interface and abstract classes. MS has also introduced the __interface keyword to native C++, but there's no such thing in the ISO C++ standard.

Abstract classes in C++ would be a class with one or more pure virtual member, such as

class foo
{
public:
virtual void bar() = 0;
};

LouArnold wrote:

2) When do you use "interface" and when do you use "abstract"

Interfaces provide absolutely no implementation. They are merely skeletons for derived classes. Abstract bases, on the other hand, may very well provide a default implementation (even pure virtual functions may provide implementation in C++.

LouArnold wrote:

ref class IConfig abstract
{
public:
IConfig(void){};
//"=0" makes pure virtual method that subclass MUST implement
virtual String ^Config()=0; //pure virtual
virtual bool Config(const String ^sConfig)=0;
};

That's an abstract base class, not an interface. As long as IConfig::Config provides a default implementation (though it can only be called by explicit qualification), it cannot be an interface.

Interface example:

interface class IConfig
{
String ^Config();
bool Config(const String ^sConfig);
};






Re: Visual C++ Language C++ "interface" and "abstract" keywords

LouArnold

Thank you. You have answered my questions. A few more please:

3) The classes declared as "interface class" - are they managed code Since they are supported by the CLR, it would imply they are.

4) To create a native abstract base class, I tried creating a class with pure virtual functions. However, I received compiler errors when I omitted the "abstract keyword" in the class declaration. I assume I must somehow compile the code with a different compiler switch





Re: Visual C++ Language C++ "interface" and "abstract" keywords

einaros

LouArnold wrote:


3) The classes declared as "interface class" - are they managed code Since they are supported by the CLR, it would imply they are.

interface class foo {} is the C++/CLI syntax. In native C++, the Microsoft extension would use __interface foo {}.

LouArnold wrote:

4) To create a native abstract base class, I tried creating a class with pure virtual functions. However, I received compiler errors when I omitted the "abstract keyword" in the class declaration. I assume I must somehow compile the code with a different compiler switch

An abstract base class in native C++ would look like any other native C++ class, but with one or more pure virtual functions within it. You don't need any extra class modifiers or compiler paramters to use it.






Re: Visual C++ Language C++ "interface" and "abstract" keywords

LouArnold

Yes, ok about #4. Here are some followup notes based on some experiments.

/* Some notes on the class declaration:

5) "ref class IConfig abstract" generates the compiler error: "cannot define an abstract
method of a managed class" if you provide default implementations for the pure virtual methods,
but works fine if default implementations are omitted.

6) The "interface class" keywords cannot be used with "ref" as in "ref interface class",
or a compile error will generated - probably because they are not instantiated at all.
The class is pure virual. The keywords access specifiers "public:", and "private:" are
not used, although no error is generated. The constructors are omitted. The term "virtual"
for methods is omitted, as is the "=0" denoting a pure virtual method.

7) "public class IConfig abstract" works file with pure virtual methods, and with default
method implementations. Still don't know if this is managed code.
*/




Re: Visual C++ Language C++ "interface" and "abstract" keywords

duck thing

LouArnold wrote:

5) "ref class IConfig abstract" generates the compiler error: "cannot define an abstract
method of a managed class" if you provide default implementations for the pure virtual methods,
but works fine if default implementations are omitted.

I suspect that if you don't provide any implementation, the compiler assumes it's an interface, even if you haven't explicitly said so.

LouArnold wrote:

6) The "interface class" keywords cannot be used with "ref" as in "ref interface class",
or a compile error will generated - probably because they are not instantiated at all.
The class is pure virual. The keywords access specifiers "public:", and "private:" are
not used, although no error is generated. The constructors are omitted. The term "virtual"
for methods is omitted, as is the "=0" denoting a pure virtual method.

1.) Correct -- "ref" and "interface" are mutually exclusive.
2.) The access specifiers do not need to be explicitly used; the default access for class members is private.
3.) Yes, omitting constructor/destructors is fine (at least in theory!) The compiler will create them for you -- an "implicit" constructor/destructor.
4.) "virtual" is redundant in an interface class's method declarations, since they never provide implementation.

LouArnold wrote:

7) "public class IConfig abstract" works file with pure virtual methods, and with default
method implementations. Still don't know if this is managed code.

The "abstract" keyword just means that you can't instantiate the class. You can have both virtual and non-virtual methods in an abstract class. I believe the abstract keyword is specific to managed code, but I haven't tried using it in unmanaged code so I'm not sure.

(I'm not an authority on C++ so I've probably made some mistakes. Any corrections are welcome!)






Re: Visual C++ Language C++ "interface" and "abstract" keywords

LouArnold

To duck thing: For my note 6, and your comment 2 about access specifiers in a class declared simply as "interface class IConfig". The default access for members is likely "public". I tried this by declaring the member access as "private" and I got the compiler error "error C2862: an interface can only have public members", and errors where the members are referenced, saying that the members were not accessible. This seems to make sense seeing as the purpose of an interface is to make members publicly accessible.

I noted you last statement about not being an expert. It explains the clarity in your explanations. I have designed software for 30 years in C++ and other languages, yet I am still learning. Keep trying :))