Is there a way to initialize a non-assigned structure declaration (or is it a definition)?
Vijay Nayar
madric at gmail.com
Tue Nov 13 12:40:18 PST 2012
This is merely a syntactic difference in how structs are handled.
In D, structs are more akin to low level types like int and have
most of the same symantics.
So
SnonParameterized cnp(5, 3.303);
makes about as much sense as
int cnp(3);
You have two syntax choices to pick from in the D version of the
code you posted:
struct SnonParameterized
{
int t;
float u;
};
void main() {
auto snon1 = SnonParameterized(3, 4.5); // Option 1
SnonParameterized snon2 = {3, 4.5}; // Option 2
}
- Vijay
On Saturday, 10 November 2012 at 17:41:00 UTC, Too Embarrassed To
Say wrote:
> I appreciate all the helpful replies, but I've simplified
> things to what I belive is the core issue. In C++ (at the risk
> of becoming a heretic) the language allows me to do the
> following:
>
> struct SnonParameterized
> {
> public:
> int t;
> float u;
> SnonParameterized(int tparam, float uparam);
> };
>
> SnonParameterized::SnonParameterized(int tparam, float uparam)
> {
> t = tparam;
> u = uparam;
> }
>
> SnonParameterized snp(5, 3.303); // this compiles with Visual
> C++ 2010
>
>
> ===============================================================================
>
> Now with D, I try (what I think is identical semantics) the
> following:
>
>
> struct SnonParameterized
> {
> int t;
> float u;
> this(int t, float u)
> {
> this.t = t
> this.u = u;
> }
> }
>
> SnonParameterized cnp(5, 3.303); // fails compile with Error:
> found 'cnp' when expecting ';' following statement
>
> auto hi = SnonParameterized(5, 3.303); // compiles of course.
>
>
> I'm just trying to understand why D disallows the
> non-assignment syntax. Probably for a very good (and obvious)
> reason.
More information about the Digitalmars-d-learn
mailing list