Hi all,
i've got a simple question: how does the default copy-constructor act
on member-arrays ? Since array-variables are often treated as pointers
to the array-element-type, one could think that the default copy-
constructor would just copy the address of the first element of the
member-array-variables.
A simple test-programm reveals, that g++-2.96 instead copies the
member-array array-element by array-element.
Please enlighten me on this topic.
Thanks in advance. Test-programm attached.
Darius.
#include <iostream>
class X {
int v[4];
public:
X *This;
X(int i1 = 0, int i2 = 0, int i3 = 0, int i4 = 0) { v[0] = i1; v[1] = i2; v[2] = i3; v[3] = i4; This = this; };
~X() {};
friend ostream& operator<< (ostream& s, const X& w) { return s << " " << w.v[0] << "[" << &w.v[0] << "] " << w.v[1] << " " << w.v[2] << " " << w.v[3]; };
};
int main(int argc, char **argv)
{
X v;
X w(1, 2, 3, 4);
X x(w);
std::cout << "v = (" << v << ")" << std::endl;
std::cout << "w = (" << w << ")" << " This = " << w.This << std::endl;
std::cout << "x = (" << x << ")" << " This = " << x.This << std::endl;
}
Output:
v = ( 0[0xbffffa20] 0 0 0)
w = ( 1[0xbffffa00] 2 3 4) This = 0xbffffa00
x = ( 1[0xbffff9e0] 2 3 4) This = 0xbffffa00 4 1437
dmoos AT esigma-systems DOT de wrote: Hi all,
i've got a simple question: how does the default copy-constructor act on member-arrays ? Since array-variables are often treated as pointers to the array-element-type, one could think that the default copy- constructor would just copy the address of the first element of the member-array-variables.
There is a misconception on your part:
Arrays are not pointers !
Only in certain circumstances, eg. when passing an array to
a function, the array decays into a pointer to its first
element.
But: Arrays are not pointers ! A simple test-programm reveals, that g++-2.96 instead copies the member-array array-element by array-element.
As it should do it.
--
Karl Heinz Buchegger kb******@gascad .at
Is it possible to initialize elements of a member array in the member
initializer list?
The reason I ask is because I defined a class X. I then defined a class
ArrayOfX which contains an array of X.
When relying on the compiler generated copy constructor for ArrayOfX the X copy
constructor was called for each element in the array of X held by the ArrayOfX
instance.
However, when I provided a copy constructor for ArrayOfX the default
constructor for each X element in the array was called.
I know how to use the member initializer list to specify construction methods
for individual objects. Can it also be used to initialize array elements?
I am unware of the syntax if it can be.
Regards
Brian F. Seaberg
Naperville, Illinois
Delray Beach, Florida
"DaKoadMunk y" <da*********@ao l.com> wrote... Is it possible to initialize elements of a member array in the member initializer list?
No.
The reason I ask is because I defined a class X. I then defined a class ArrayOfX which contains an array of X.
When relying on the compiler generated copy constructor for ArrayOfX the X
copy constructor was called for each element in the array of X held by the
ArrayOfX instance.
However, when I provided a copy constructor for ArrayOfX the default constructor for each X element in the array was called.
I know how to use the member initializer list to specify construction
methods for individual objects. Can it also be used to initialize array elements?
No.
I am unware of the syntax if it can be.
There isn't any.
Victor
Thanks for the clarification. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rich |
last post by:
Hi,
I have a query regarding VC6 and its handling of templated copy
constructors. Here goes:
Take a look at the following code sample...
template<class _Ty, size_t t_uiSize = 10 >
class my_template
|
by: mescaline |
last post by:
//Consider the simple program with inheritance, plain init for A, copy ctr for B
#include <iostream>
using namespace std;
class Base{
public:
Base(){cout << "Base, default" << endl;}
Base(const Base &){cout << "Base, Copy Ctor" << endl;}
};
|
by: Sam Wilson [Bentley] |
last post by:
If you pass a C++ object by value as an argument to a function which has
a variable-length argument list (...), the MSVC7 C++ compiler does not
call the object's copy constructor and will not complain if the copy
constructor is private.
1) Is this part of the C++ language definition? What is the thinking
behind it?
2) In any case, how can I catch at compile time any callers that try to
|
by: away |
last post by:
1. When a class defined with a member of pointer type, it's necessary to
have a copy constructor and assignment operator.
If don't pass objects of such class as value in a function and don't do
assignment, should copy constructor and assignment operator be unnecessary?
2. If a base class have a pointer member, should its derived classes also
need copy constructor and assignment operator, no matter if a derived class
itself has a...
|
by: sd2004 |
last post by:
could someone please show/help me to copy all element from "class dog"
to "class new_dog" ?
Note: "class new_dog" has new element "age" which should have value
"my_age"
/////////////////////////////// source code
///////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
| |
by: blangela |
last post by:
3.0 Advanced Topic Addendum
There are a few cases where the C++ compiler cannot provide an
overloaded assignment operator for your class. If your class contains
a const member or/and a reference member, the compiler will not be able
to synthesize an assignment operator for your class. It actually helps
to think of a reference member as a const member (since it cannot be
made to reference any other object once it has been initialized).
...
|
by: John Salmon |
last post by:
g++ complains about illegal access to a private member
when the following is compiled with a private copy
constructor for the class C. When the copy constructor
is public, the program runs and demonstrates(?) that
the copy constructor is never called (at least no sign
of its chatter on std::cout is visible).
So - is it necessary to have a public copy constructor
in order to use "function style" initializers for an
array of objects? ...
|
by: campos |
last post by:
"Effective C++ 3rd Edition" Item 6, P39
-------------------------------------------------------
class Uncopyable {
protected: // allow construction
Uncopyable() {} // and destruction of
~Uncopyable() {} // derived objects...
private:
Uncopyable(const Uncopyable&); // ...but prevent copying
|
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
using namespace std;
class Base
{
public:
Base(int x = 0);
|
by: Dijkstra |
last post by:
Hi folks!
First, this is the code I'm using to expose the problem:
------------------------------------------------------------------
#include <functional>
#include <string>
#include <iostream>
using namespace std;
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |