Topic: struct = struct?


Author: Ron Natalie <ron@spamcop.net>
Date: Mon, 8 Jan 2001 16:47:45 GMT
Raw View

rizz wrote:

>
> s2 = s1;
>
> is this assignment an ansi C++ standard?
>
It is legal in C++.  It's even legal in C since about
1979 or so.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: rizz <westphal@mandli.com>
Date: Fri, 5 Jan 2001 01:01:26 GMT
Raw View
I'm wondering if it is an ansi standard in C++ to make a structure equal
another structure?

example:

typedef struct myStruct
{
    short a;
    long b;
}myStruct;

myStruct s1, s2;

s1.a = 4;
s1.b = 6;

s2 = s1;

is this assignment an ansi C++ standard?

thanks


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]





Author: James Dennett <james@evtechnology.com>
Date: Fri, 5 Jan 2001 01:17:58 GMT
Raw View
rizz wrote:
>
> I'm wondering if it is an ansi standard in C++ to make a structure equal
> another structure?

It's in the (ISO) C++ Standard, and it was in the C Standard before that.
(The C++ standard has much more to say about assignment though.)

> example:
>
> typedef struct myStruct
> {
>     short a;
>     long b;
> }myStruct;

In C++, you don't need a typedef as well as the struct definition.
Just "struct myStruct { short a; long b; };" will do the job.

> myStruct s1, s2;
>
> s1.a = 4;
> s1.b = 6;
>
> s2 = s1;
>
> is this assignment an ansi C++ standard?

Yes.  It works in C++, and in C.

In C++ you can define a copy assignment operator (the "=" in "s2 = s1")
for myStruct yourself, but if you don't then the compiler will synthesize
one which performs memberwise copy, as if you had written

myStruct & operator = (const myStruct &other) {
  a = other.a;
  b = other.b;
}

(full details of constness elided for brevity).

-- James Dennett <jdennett@acm.org>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]