Re: Explicitly calling constructors
* Bart van Ingen Schenau:
Alf P. Steinbach wrote:
* Rolf Magnus:
Alf P. Steinbach wrote:
struct Foo { int x; };
int main()
{
Foo();
}
What you probably meant was that if a class has at least one
declared constructor, then, without using very low level features to
circumvent this guarantee, creating any instance of that class calls
a constructor of that class,
No. What I meant is that it's just the other way round.
There is no "other way around": you can't have one without the other.
As far as I can see, you both are using different phrases to say the
same thing: If a type has a (non-trivial) constructor, creating an
object implies that a constructor gets invoked, and equivalently if a
constructor has been invoked, an object is being created.
You cannot
circumvent it, since if the constructor hasn't been executed, it's
not an object of that type yet.
This shows that you have some fundamental misunderstanding.
I don't see a misunderstanding here.
That could possibly be because you're jumping into some debate and have no idea
of the context.
Can you show how to create an object of the non-POD type std::string
without invoking one of its constructors?
First, I don't think that this has anything to do with Rolf Magnus' mixup of
levels or context or whatever his very persistent confusion is or was about.
And second, since the question is practically meaningless (see below), it may be
that you have misunderstood something, different from Rolf Magnus though.
But, if you're referring to the low-level features I mentioned, the validity of
any solution depends on the particular implementation of std::string. Such code,
reproducing a memory layout, is not portable, and is "very low-level", and since
you're asking about std::string, for which such techniques would be wholly
inappropriate no matter the context (this is why the question is practically
meaningless), my advice is to not do it, and to not even think about it.
On the other hand, in contrast to std::string and a practitioner who doesn't
know that std::string isn't relevant, it can be trivial and useful for someone
who does have the requisite understanding and who controls the class.
E.g. such techniques are used in Microsoft's libraries (not sure if they do it
for binary serialization, but e.g. downcasting for access control is, as I
recall, used in ATL).
Cheers & hth.,
- Alf