473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Object instantiation shortcuts

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
10 1819
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

"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
> 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

"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
> >> 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

"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
> >> >> 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

"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

"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
> > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
0
by: GeorgeF | last post by:
How can I conditionally deploy shortcuts to my application? I have created a custom installer dialog box to give my users the option of creating desktop/start menu shortcuts, but the shortcuts do...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
7
by: John A Grandy | last post by:
I'm trying to get a decent idea of the relative performance of three types of implementations of data-access classes in ASP.NET 2.0. I believe this boils down to a more basic question regarding...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
2
by: WittyGuy | last post by:
Hi My class looks something like this: class Base { public: Base () {} ~Base () {} // No virtual dtor in the base class private: };
3
FishVal
by: FishVal | last post by:
Windows Script Host Object library. Full name: Windows Script Host Object Model LibName: IWshRuntimeScripting Location: ...\WINDOWS\system32\wshom.ocx The present tip is closely related to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.