Hannu said:
Ioannis Vranos said:
namespaces for
modular programming. Does C++ support OO "completely"? It has no
metaclasses, no multidispatch, classes aren't objects nor does it have
reflection [1].
All these are features of CLI
Though I wasn't talking about CLI, AFAIK that's not true. But show me
wrong. (I know reflection is a feature of CLI)
For consistency I will use the C++/CLI standard syntax that will be used
in the upcoming version of Visual Studio 2005 and afterwards.
About classes not being objects, yes in C++ types themselves are not
objects and do not occupy space, and I think this is very reasonable.
But they do have static methods though.
Now about metaclasses, I assume you talk about attributes.
using namespace System;
// I create a custom attribute
[AttributeUsageAttribute(AttributeTargets::Class)]
public ref class SomeAttribute: public Attribute
{
public:
SomeAttribute(String ^) {}
property int Something;
};
// A class using this attribute
[SomeAttribute("Text", Something=7)]
ref class TestClass
{
};
int main()
{
TestClass obj;
}
Reflection is library support. We get a Type object etc.
Then I'm sure you can describe me which keywords enable each of those
features, because I'm quite sceptical of this one as well. I wouldn't
call it C++ support either if only one compiler happened to implement
some feature.
In the above code I created a trivial property, where the compiler
produces its definition.
In any case to avoid this tiring thing, have you checked this Microsoft
presentation?
http://microsoft.sitestream.com/TechEd/DEV/DEV333_files/Botto_files/DEV333_Sutte.ppt
C++ becomes the systems programming language of CLI and .NET, so C++ can
do all things C# can, while the opposite is not true.
Have you automatic Dispose definition in C#? Objects in the stack?
Compile time definition?
Can you do this in C#?
ref class ReferenceType
{
int i;
public:
ReferenceType():i(1) {}
ReferenceType(const ReferenceType %x) { i=x.i; }
void print() { System::Console::WriteLine(i); }
};
template <class T>
void display(T x)
{
x.print();
}
int main()
{
// Object with stack semantics - Deterministic destruction
// at the end of its scope
ReferenceType obj;
display(obj);
// Object in the managed heap
ReferenceType ^hobj= gcnew ReferenceType;
display(*hobj);
// Deterministic destruction
delete hobj;
ReferenceType ^hobj2= gcnew ReferenceType;
display(*hobj2);
// Not destroying hobj2, let it be finalised
}
C:\c>cl /clr:safe temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40809
for Microsoft (R) .NET Framework version 2.00.40607.16
Copyright (C) Microsoft Corporation. All rights reserved.
temp.cpp
Microsoft (R) Incremental Linker Version 8.00.40809
Copyright (C) Microsoft Corporation. All rights reserved.
/out:temp.exe
temp.obj
C:\c>temp
1
1
1
C:\c>
That is 100% verifiable code.
The C++/CLI draft 1.5 has no mention of metaclasses or multidispatch.
I have not read it but I did a quick search. I doubt one can just pass
classes around like objects, either. Maybe through some wrappers.
Reflection is there though.
You mean passing classes to other functions? Easy. Can you do this in C#?
ref class ReferenceType
{
public:
static void somefun() { System::Console::WriteLine("Hey\n"); }
};
ref class ReferenceType2
{
public:
static void somefun() { System::Console::WriteLine("Hey2\n"); }
};
template <class T>
void display()
{
T::somefun();
}
int main()
{
display<ReferenceType>();
display<ReferenceType2>();
}
C:\c>cl /clr:safe temp.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40809
for Microsoft (R) .NET Framework version 2.00.40607.16
Copyright (C) Microsoft Corporation. All rights reserved.
temp.cpp
Microsoft (R) Incremental Linker Version 8.00.40809
Copyright (C) Microsoft Corporation. All rights reserved.
/out:temp.exe
temp.obj
C:\c>temp
Hey
Hey2
C:\c>
And last but not least, when I was talking about C++, I was talking
about *the* C++ (1998 standard), not C++/CLI, or any non-standard
extensions to C++. Much like when I say C, I don't mean C++.
You don't get it. C++ standard itself is the core compile-time language
defining portability.
System specific stuff like CLI Virtual Machines, you will use those
facilities.
I use C++/CLI as you use C#/CLI (there is no a C# standard alone).
For example you can't use C# in a non-CLI environment, while ISO C++
code compiles everywhere, both in CLI and non CLI environments.