By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,594 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,594 IT Pros & Developers. It's quick & easy.

Object instantiation shortcuts

P: n/a
would it be possible to allow an extended syntax for Object instantiation so
that instead of

Hashtable<string,int> table = new Hashtable<string,int>(10, 10f);

I can write:

Hashtable<string,int> table(10, 10f);

Would this be possible or is this ambiguous to the compiler?

In some situations it would be very useful especially when using generics.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
cody <pl*************************@gmx.de> wrote:
would it be possible to allow an extended syntax for Object instantiation so
that instead of

Hashtable<string,int> table = new Hashtable<string,int>(10, 10f);

I can write:

Hashtable<string,int> table(10, 10f);

Would this be possible or is this ambiguous to the compiler?

In some situations it would be very useful especially when using generics.


I really hope this *doesn't* happen - simply because at the moment,
it's very clear when you're explicitly creating a new object. Is the
difference in the amount of typing really that important?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a

"cody" <pl*************************@gmx.de> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
would it be possible to allow an extended syntax for Object instantiation
so
that instead of

Hashtable<string,int> table = new Hashtable<string,int>(10, 10f);

I can write:

Hashtable<string,int> table(10, 10f);

Would this be possible or is this ambiguous to the compiler?

In some situations it would be very useful especially when using generics.


Its possible but probably undesireable. As Jon said, it doesn't make
creation of a new instance possible and I think it has a different meaning
in C++(stack construction of a local or explict conversions or something,
its one of those little tidbits of C++ I never got around to figuring out
that helps makes the language insanely complex).

Anyway, a decent IDE fixes this, vs2005 already automatically inserts
everything up to the first ( when you hit the right key(don't have it
infront ofm e, not sure of the key). This is probably a "tools" problem more
so than a language one. I'll give you that you can't always use IDE's, but I
would suspect most of hte time you can and should.
Nov 16 '05 #3

P: n/a
> Its possible but probably undesireable.

Contribute stupid proposals are my specialitiy :)
As Jon said, it doesn't make
creation of a new instance possible and I think it has a different meaning
in C++(stack construction of a local or explict conversions or something,
its one of those little tidbits of C++ I never got around to figuring out
that helps makes the language insanely complex).


Iam not sure what you mean with explicit conversion, but in C++ this syntax
only calls the ctor for the given variable.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #4

P: n/a

"cody" <pl*************************@gmx.de> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl...
Its possible but probably undesireable.
Contribute stupid proposals are my specialitiy :)
As Jon said, it doesn't make
creation of a new instance possible and I think it has a different
meaning
in C++(stack construction of a local or explict conversions or something,
its one of those little tidbits of C++ I never got around to figuring out
that helps makes the language insanely complex).


Iam not sure what you mean with explicit conversion, but in C++ this
syntax
only calls the ctor for the given variable.


I can't find documentation on it, however while the C++ syntax calls it for
the given variable, doesn't it only work for stack allocated variables? --
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 16 '05 #5

P: n/a
> >> As Jon said, it doesn't make
creation of a new instance possible and I think it has a different
meaning
in C++(stack construction of a local or explict conversions or something, its one of those little tidbits of C++ I never got around to figuring out that helps makes the language insanely complex).
Iam not sure what you mean with explicit conversion, but in C++ this
syntax
only calls the ctor for the given variable.


I can't find documentation on it, however while the C++ syntax calls it

for the given variable, doesn't it only work for stack allocated variables?

Yes the syntax

CFile f();

works only for local/static variables, because it doesn't allocate memory,
it only calls a constructor. For dynamic allocation you have to use

CFile * f = new CFile();

Note that you have to * to mark to variable as a pointer.

If you have a CFile in another class as instance variable you have to
initialize it in the ctor of the enclosing class in the ctor's initializer
list:

class FileWrapper
{
CFile f;
public:
FileWrapper(const char* fname);
};

// this is the ctor
void FileWrapper::FileWrapper(const char* fname)
: f(fname)
{

}
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #6

P: n/a

"cody" <pl*************************@gmx.de> wrote in message
news:uY**************@TK2MSFTNGP09.phx.gbl...
>> As Jon said, it doesn't make
>> creation of a new instance possible and I think it has a different
>> meaning
>> in C++(stack construction of a local or explict conversions or something, >> its one of those little tidbits of C++ I never got around to figuring out >> that helps makes the language insanely complex).
>
> Iam not sure what you mean with explicit conversion, but in C++ this
> syntax
> only calls the ctor for the given variable.
>


I can't find documentation on it, however while the C++ syntax calls it

for
the given variable, doesn't it only work for stack allocated variables?

Yes the syntax

CFile f();

works only for local/static variables, because it doesn't allocate memory,
it only calls a constructor. For dynamic allocation you have to use


As I thought. My opinion of C++ isn't high so I never bothered to learn much
beyond dynamic allocation. Quite fond of C, but I just can't stand the
design of C++ pretty much right down to where C meets C++.


Nov 16 '05 #7

P: n/a
> >> >> As Jon said, it doesn't make
>> creation of a new instance possible and I think it has a different
>> meaning
>> in C++(stack construction of a local or explict conversions or something,
>> its one of those little tidbits of C++ I never got around to
figuring out
>> that helps makes the language insanely complex).
>
> Iam not sure what you mean with explicit conversion, but in C++ this
> syntax
> only calls the ctor for the given variable.
>

I can't find documentation on it, however while the C++ syntax calls it

for
the given variable, doesn't it only work for stack allocated variables?

Yes the syntax

CFile f();

works only for local/static variables, because it doesn't allocate memory, it only calls a constructor. For dynamic allocation you have to use


As I thought. My opinion of C++ isn't high so I never bothered to learn

much beyond dynamic allocation. Quite fond of C, but I just can't stand the
design of C++ pretty much right down to where C meets C++.

In my days as C++ programmer I liked C++ very much but looking back now I
consider the design of both C and C++ as inherently broken and now, spoiled
from Java and C#, I cannot imagine how to program with a language where
nothing holds you from accessing uninitialized or already freed memory or
you can easily have pointers to local variables and when using them outside
the method..boom :)

I had a few day ago a look at the inofficial successor language of C++, D.
Looks very very interesting. It already has templates and borrows lots of
features from C#.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #8

P: n/a

"cody" <pl*************************@gmx.de> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
>> >> As Jon said, it doesn't make
>> >> creation of a new instance possible and I think it has a different
>> >> meaning
>> >> in C++(stack construction of a local or explict conversions or
> something,
>> >> its one of those little tidbits of C++ I never got around to figuring > out
>> >> that helps makes the language insanely complex).
>> >
>> > Iam not sure what you mean with explicit conversion, but in C++ this
>> > syntax
>> > only calls the ctor for the given variable.
>> >
>>
>> I can't find documentation on it, however while the C++ syntax calls
>> it
> for
>> the given variable, doesn't it only work for stack allocated
>> variables?
>
>
> Yes the syntax
>
> CFile f();
>
> works only for local/static variables, because it doesn't allocate memory, > it only calls a constructor. For dynamic allocation you have to use
As I thought. My opinion of C++ isn't high so I never bothered to learn

much
beyond dynamic allocation. Quite fond of C, but I just can't stand the
design of C++ pretty much right down to where C meets C++.

In my days as C++ programmer I liked C++ very much but looking back now I
consider the design of both C and C++ as inherently broken and now,
spoiled
from Java and C#, I cannot imagine how to program with a language where
nothing holds you from accessing uninitialized or already freed memory or
you can easily have pointers to local variables and when using them
outside
the method..boom :)

I started with C. C I could handle without any substantial errors(the odd
double-freed pointer or unchecked error code), however with C++ I seem to
*always* blow my leg off. I've had more issues with templates than I
probably have had with windows iin my entire life.
I had a few day ago a look at the inofficial successor language of C++, D.
Looks very very interesting. It already has templates and borrows lots of
features from C#.

Ya, I think D is pretty interesting. I havn't had time to learn it but its
on the top of the list. Not sure I'll enjoy it too much since it is still a
bit too close to C++ in some respects, but its got to better than C++.
Nov 16 '05 #9

P: n/a

"cody" <pl*************************@gmx.de> wrote in message
news:uY**************@TK2MSFTNGP09.phx.gbl...
> As Jon said, it doesn't make
> creation of a new instance possible and I think it has a different
> meaning
> in C++(stack construction of a local or explict conversions or something,> its one of those little tidbits of C++ I never got around to figuring out> that helps makes the language insanely complex).

Iam not sure what you mean with explicit conversion, but in C++ this
syntax
only calls the ctor for the given variable.

I can't find documentation on it, however while the C++ syntax calls it

for
the given variable, doesn't it only work for stack allocated variables?

Yes the syntax

CFile f();


That's a function prototype in C++ (a function named 'f', taking no
arguments, returning a CFile).

I think you meant to say

CFile f;

which allocates and constructs a local variable of type CFile, named f, on
the stack.

Stu

works only for local/static variables, because it doesn't allocate memory,
it only calls a constructor. For dynamic allocation you have to use

CFile * f = new CFile();

Note that you have to * to mark to variable as a pointer.

If you have a CFile in another class as instance variable you have to
initialize it in the ctor of the enclosing class in the ctor's initializer
list:

class FileWrapper
{
CFile f;
public:
FileWrapper(const char* fname);
};

// this is the ctor
void FileWrapper::FileWrapper(const char* fname)
: f(fname)
{

}
--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Nov 16 '05 #10

P: n/a
> > CFile f();

That's a function prototype in C++ (a function named 'f', taking no
arguments, returning a CFile).

I think you meant to say

CFile f;

which allocates and constructs a local variable of type CFile, named f, on
the stack.

It is too long ago..
I remember, in the case if no arguments are present the compiler cannot
distinguish between function proptotype and local variable.
If arguments are present the compiler can distinguish between function
prototype:

CFile f (const char*);

and local variable declaration:

CFile f (fname);

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.