Object instantiation shortcuts

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

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 said:
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?
 
cody said:
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.
 
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 said:
Contribute stupid proposals are my specialitiy :)


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
 
As Jon said, it doesn't make
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 said:
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++.
 
As Jon said, it doesn't make
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 said:
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++.
 
cody said:
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
 
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);
 
Back
Top