C++/CLI : How do I declare abstract (in C#) class and method in C++/CLI?
Asked Answered
H

2

11

What is the equivalent of the following C# code in C++/CLI?

public abstract class SomeClass
{
    public abstract String SomeMethod();
}
Homeless answered 5/12, 2009 at 1:33 Comment(0)
L
22

Just mix up the keywords a bit to arrive at the correct syntax. abstract goes in the front in C# but at the end in C++/CLI. Same as the override keyword, also recognized today by C++11 compliant compilers which expect it at the end of the function declaration. Like = 0 does in traditional C++ to mark a function abstract:

public ref class SomeClass abstract {
public:
  virtual String^ SomeMethod() abstract;
};
Lesslie answered 5/12, 2009 at 1:37 Comment(2)
Is there any difference between declaring "SomeMethod() = 0" and "SomeMethod() abstract"?Homeless
No. = 0 is C++ syntax but C++/CLI supports it too.Lesslie
S
7

You use abstract:

public ref class SomeClass abstract
{
    public:
        virtual System::String^ SomeMethod() = 0;
}
Spermaceti answered 5/12, 2009 at 1:35 Comment(2)
Is there any difference between declaring "SomeMethod() = 0" and "SomeMethod() abstract"?Homeless
Nope. The Method() = 0 is the non-C++/CLI (just stnadard C++) way of defining an abstract class. With C++/CLI, you can use it, or the new abstract keyword. I prefer using the original, since it's just habit, and the abstract keyword is context sensitive in the case of a method, but either works. See: msdn.microsoft.com/en-us/library/b0z6b513(VS.80).aspxSpermaceti

© 2022 - 2024 — McMap. All rights reserved.