473,667 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default parameter for member array?

As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

struct A{
A(char (&array)[16] ){
std::copy(&arra y[0],&array[16],&_array[0]);
}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._a rray<<std::endl ;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 6 '05 #1
16 2563
* Steven T. Hatton:
As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

char z[16] = {};

struct A{
A(char (&array)[16] ){
A( char (&array)[16] = z )

std::copy(&arra y[0],&array[16],&_array[0]);
You know, names that differ only in an underscore are not visually
distinctive.

}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._a rray<<std::endl ;
}


int main() {
A a;
std::cout<<a._a rray<<std::endl ;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 6 '05 #2
Alf P. Steinbach wrote:
* Steven T. Hatton:
As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

char z[16] = {};


Too bad I can't have that as the default operation performed on a POD class
when it's constructed. AFAIK, I can do this.

struct C { int _a; int _b; };

C c = {};

To reliably zero-initialize all the members of a POD class. But I don't see
a way to use that for the default constructor. Writing out all the members
in the initialization list can be tedious.

struct A{
A(char (&array)[16] ){


A( char (&array)[16] = z )

std::copy(&arra y[0],&array[16],&_array[0]);


You know, names that differ only in an underscore are not visually
distinctive.


Actually, I was being sloppy. Typically I will do this:

struct A{
A(char (&array_)[16] )
{std::copy(&arr ay_[0],&array[16]_,&_array[0]);}
char _array[16];
};

Which in this case doesn't help a lot because the syntax is so busy. In
general, however, since I frequently use _member for the member
declaration, and member_ for a parameter, I'm conditioned to look for them.
It works fairly well. The only symbol that really causes me much problem
is '#'.
}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._a rray<<std::endl ;
}


int main() {
A a;
std::cout<<a._a rray<<std::endl ;
}


Interestingly, if the parameter is not const it won't accept a const char[]
for z, but as far as I can tell, z and A::_array are two distinct objects,
so z would never be modified as a result of that initialization. Typically
the parameter to the constructor should probably be const anyway. I also
found that I can make the default initializer a static member, which
satisfies my desire for proper containment.

This code show some behavior that was not obvious from looking at the
syntax.

#include <iostream>
#include <string>
std::string s("surprise!" );

class A {
static const char s_Z[16];
public:
A(const char (&array_)[16] = s_Z ) {
std::copy(&arra y_[0],&array_[16],&_array[0]);
}
void surprise() { std::copy(&s[0],&s[s.size()],&_array[0]); }
char _array[16];
};

const char A::s_Z[16] = {'1','2','3','4 ','5','6','7',' 8','9'
,'A','B','C','D ','E','F','\0'} ;

char z[16] = {};

struct B {
B( const char (&array_)[16] = z ) {
std::copy(&arra y_[0],&array_[16],&_array[0]);
}
char _array[16];
};

int main() {
char arr[16] = {'1','2','3','4 ','5','6','7',' 8','9'
,'a','b','c','d ','e','f','\0'} ;
A a(arr);
A aa;
std::cout<<a._a rray<<std::endl ;
a.surprise();
std::cout<<aa._ array<<std::end l;
std::cout<<a._a rray<<std::endl ;
std::cout<<z<<s td::endl;
B b;
std::copy(&s[0],&s[s.size()],&z[0]);
B bb;
std::cout<<b._a rray<<std::endl ;
std::cout<<bb._ array<<std::end l;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 7 '05 #3
* Steven T. Hatton:
[someething]


?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '05 #4
Alf P. Steinbach wrote:
* Steven T. Hatton:
[someething]


?


This sucks!

struct Elf32_Ehdr {
Elf32_Ehdr(unsi gned char (&e_ident_)[EI_NIDENT] = sc_e_ident
,Elf32_Half e_type_ = 0
,Elf32_Half e_machine_ = 0
,Elf32_Word e_version_ = 0
,Elf32_Addr e_entry_ = 0
,Elf32_Off e_phoff_ = 0
,Elf32_Off e_shoff_ = 0
,Elf32_Word e_flags_ = 0
,Elf32_Half e_ehsize_ = 0
,Elf32_Half e_phentsize_ = 0
,Elf32_Half e_phnum_ = 0
,Elf32_Half e_shentsize_ = 0
,Elf32_Half e_shnum_ = 0
,Elf32_Half e_shstrndx_ = 0)
:e_type(e_type_ )
,e_machine(e_ma chine_)
,e_version(e_ve rsion_)
,e_entry(e_entr y_)
,e_phoff(e_phof f_)
,e_shoff(e_shof f_)
,e_flags(e_flag s_)
,e_ehsize(e_ehs ize_)
,e_phentsize(e_ phentsize_)
,e_phnum(e_phnu m_)
,e_shentsize(e_ shentsize_)
,e_shnum(e_shnu m_)
,e_shstrndx(e_s hstrndx_)
{ std::copy(&e_id ent_[0],&e_ident_[EI_NIDENT],&e_ident[0]); }

static const char sc_e_ident[EI_NIDENT];//for initialization. See
Types.cpp.

unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};
#include <sth/elf32/Types.h>

namespace sth {
namespace elf32 {
const char Elf32_Ehdr::sc_ e_ident[EI_NIDENT]={};
}
}

Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 7 '05 #5
* Steven T. Hatton:
Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.


Still not sure what you mean.

Is your question: how do I zero a POD member in a constructor initializer
list?

In that case, for POD member m and class C, "C(): m() {}".

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '05 #6
Alf P. Steinbach wrote:
* Steven T. Hatton:
Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.


Still not sure what you mean.

Is your question: how do I zero a POD member in a constructor initializer
list?

In that case, for POD member m and class C, "C(): m() {}".


No, that wasn't the question. But that certainly is cleaner than what I
showed. I just figured if I was going to bother writing the initializers,
I might as well make them more useful. My question is how do I accomplish
what the following does, but with something as simple as Elf32_Ehdr eh={}:

// ELF file header
struct Elf32_Ehdr {
Elf32_Ehdr():e_ type ()
,e_machine ()
,e_version ()
,e_entry ()
,e_phoff ()
,e_shoff ()
,e_flags ()
,e_ehsize ()
,e_phentsize()
,e_phnum ()
,e_shentsize()
,e_shnum ()
,e_shstrndx ()
{}
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};

Actually, what's above is probably the closest you can get to what I want.
It seems like it would be relatively trivial to provide some syntactic
legerdemain to get the compiler to zero initialize all members by default
with one or two characters.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Aug 7 '05 #7
* Steven T. Hatton:
Alf P. Steinbach wrote:
* Steven T. Hatton:
Yes, I know I can do Elf32_Ehdr hdr = {};, but I want that to happen
automatically.


Still not sure what you mean.

Is your question: how do I zero a POD member in a constructor initializer
list?

In that case, for POD member m and class C, "C(): m() {}".


No, that wasn't the question. But that certainly is cleaner than what I
showed. I just figured if I was going to bother writing the initializers,
I might as well make them more useful. My question is how do I accomplish
what the following does, but with something as simple as Elf32_Ehdr eh={}:

// ELF file header
struct Elf32_Ehdr {
Elf32_Ehdr():e_ type ()
,e_machine ()
,e_version ()
,e_entry ()
,e_phoff ()
,e_shoff ()
,e_flags ()
,e_ehsize ()
,e_phentsize()
,e_phnum ()
,e_shentsize()
,e_shnum ()
,e_shstrndx ()
{}
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
};


struct Elf32: Elf32_Ehdr{ Elf32(): Elf32_Ehdr() {} };

OK?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '05 #8
Steven T. Hatton sade:
Actually, what's above is probably the closest you can get to what I want.
It seems like it would be relatively trivial to provide some syntactic
legerdemain to get the compiler to zero initialize all members by default
with one or two characters.


Doesn't the compiler zero-initialize (as a part of default-initializing)
every member for which no other intializer is specified?

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Aug 7 '05 #9
* Tobias Blomkvist:
Steven T. Hatton sade:
Actually, what's above is probably the closest you can get to what I want.
It seems like it would be relatively trivial to provide some syntactic
legerdemain to get the compiler to zero initialize all members by default
with one or two characters.


Doesn't the compiler zero-initialize (as a part of default-initializing)
every member for which no other intializer is specified?


Short answer: No.

Longer answer: it depends on how and where the instance is created, e.g.

auto T o;

will not initialize o when T is a POD.

Even longer answer: the rules in C++98 were not properly implemented by all
compilers, and in C++03 new rules (value-initialization) were added, so it's
rather complex; it's not a good idea to count on the standard's rules here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
12696
by: earl | last post by:
class temp { public: temp(); foo(char, char, char*); private: char matrix; }; temp::foo(char p, char o, char m = matrix )
4
4726
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
2
3072
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node //Êý¾Ý±íÖнáµãÀàµÄ¶¨Òå
3
2188
by: danilo.horta | last post by:
Hi guys I'm trying to accomplish a slightly difficult task. I think it's more easy to explain trought an unworking code: template<class T, size_t numDim> VecBasis {/*...*/}; typedef VecBasis<float, 3> vec3; template<class T, size_t size> class MatBasis { //...
10
8545
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
22
2428
by: sujilc | last post by:
This question seems to be silly. Can ony one figure out the default functions implemented by compiler when we decalre a class like class A { } According to me this declaration will define default functions like
13
1998
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is never changed. I have another class which is a subclass of the "Base" class called "Derived". This derived class has a member variable called "m_derived". "m_derived" is initialized to "2" and is never changed. I pass an array of "Base" classes...
12
2316
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include <iostream> using namespace std; template <class ClassB> class ClassA
1
3425
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to it. Therefore I wanted to creat a class member which represents the pointer to the array. Unfortunately I get the folowring compile error for the code beneath: error C3229: 'DataType *' : indirections on a generic type parameter
0
8367
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,...
0
8889
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, 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...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8650
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 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...
1
6206
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 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...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
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...

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.