473,769 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class object initialisation

A
Hi,

I have always been taught to use an inialization list for initialising data
members of a class. I realize that initialsizing primitives and pointers use
an inialization list is exactly the same as an assignment, but for class
types it has a different effect - it calls the copy constructor.

My question is when to not use an initalisation list for initialising data
members of a class?
Regards
Adi

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 1/09/2003
Jul 19 '05
106 5600
Alexander Terekhov wrote:
Attila Feher wrote:

Alexander Terekhov wrote:
White Wolf wrote:
I can copy it around. (at lest I hope so).

Nope. You can't copy it AS POINTER. std::vector<uns igned char> will
work, though. ;-)


Not even as void?


void pointer? It will also not work. You'll hit undefined
behavior as soon as you try to 'use' deleted/freed pointer
as pointer...


But that was the *whole* point: I do *not* want to use it as a pointer. I
never want to dereference it or apply arithmetics to it.

--
Attila aka WW
Jul 19 '05 #101
tom_usenet wrote:
Nope. You can't copy it AS POINTER. std::vector<uns igned char> will
work, though. ;-)


Not even as void?


This is illegal, even though it isn't dereferenced:

void* p = ::operator new(1);
operator delete(p);

void* q = p; // boom

You aren't allowed to convert a pointer to an rvalue after deleting
it. You can of course reassign it:
p = 0; //or whatever.


Ahha. Makes sense, but quite restrictive. So basically you will need to
store it as "whatever", but not as a pointer if you want to keep the value
around.

--
Attila aka WW
Jul 19 '05 #102

Attila Feher wrote:
[...]
Ahha. Makes sense, but quite restrictive. So basically you will need to
store it as "whatever", but not as a pointer if you want to keep the value
around.


Even if you store it BEFORE free-ing... since the representation
may have quite a few garbage bits, storing it as "whatever" will
be nothing but "absolutely pointless exercise"... well, just a
few exceptions (I have a patent appl on one of those ;-) ) aside,
of course.

regards,
alexander.
Jul 19 '05 #103

"Gary Labowitz" <gl*******@comc ast.net> wrote in message
news:sj******** ************@co mcast.com...
"jeffc" <no****@nowhere .com> wrote in message
news:3f******** @news1.prserv.n et...

Check what you wrote 2 posts ago. You're going around in circles.


It's called a loop.


Just trying to give the guy a break.
(get it?)
Jul 19 '05 #104
Alexander Terekhov wrote:
Attila Feher wrote:
[...]
Ahha. Makes sense, but quite restrictive. So basically you will
need to store it as "whatever", but not as a pointer if you want to
keep the value around.


Even if you store it BEFORE free-ing... since the representation
may have quite a few garbage bits, storing it as "whatever" will
be nothing but "absolutely pointless exercise"... well, just a
few exceptions (I have a patent appl on one of those ;-) ) aside,
of course.


Since looking at them later is a "platform dependent exercise" I believe
that whatever garbage bits they have it does not really matter at all.

class DeadPtr {
unsigned char bitz[sizeof(void*)]; // Could be char *
public:
DeadPtr( void const* p) { memcpy and so on}
DeadPtr( void const* const p) { memcpy and so on}
// Something special/platform dependent to display it
};

Off course ;-) this thingy has to be made _before_ the delete is being
called. I dunno if passing as reference counts as use as rvalue... if not
that can be done.

--
Attila aka WW
Jul 19 '05 #105
Gary Labowitz wrote:
[SNIP]
Well, I like your obfuscation! I didn't understand anything! It's
ready for publication!
On the serious side, the fact that there are invalid combinations of
bits for types introduces a whole new complexity for me, and makes
the standard almost unbearably complex. Oh well, reality must be
faced.
On the whole, I rather prefer Java: nine simple types to learn about,
all well defined with regard to bit usage and size, no surprises.


Nine types? No surprises? I dunno, but whenever I had to try to find
something in the Java libraries (find+understan d) I finally gave up. It is
like having to talk to all Chineese and Indian people to find a street
address. Messy.

Anyways. C++ is a general purpose programming language with systems
programming in mind. Comparing it to Java is like comparing apples to the
moon.

--
WW aka Attila
Jul 19 '05 #106
Gary Labowitz wrote:
Thank you. If this be the case, then all my musings are incorrect.
Hmm... check sum bits in a value. Yes, it could. Then invalid bit
combinations could exist. All resulting in undefined behavior I
should think.
Yep. In fact of life it is quite unusual for the "common people" to find a
HW which works like this. Anyways using a garbage value is not a really
good thing to do, even if one can get away with it without a crash.
Back to the drawing board!!!
[Gee, what did we go back to before they invented drawing boards?]


Me go bangbang wife. :-) Or maybe: back to the cave wall. :-)

--
WW aka Attila
Jul 19 '05 #107

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

Similar topics

1
3939
by: matro | last post by:
hi there, I have the following struct in my class: class CTbStDialog : public CDialog { public: int fooVar, fooVar2; ... //other foo variables...
8
2535
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class static variables) is initializated (constructed) before main is invoked . . ." .. . .
11
2558
by: Neil Zanella | last post by:
Hello, When an "std::map<int, int> foobar;" statement appears in a C++ program followed by a statement of the form "foobar;" where nothing has ever been inserted at position 123 into map foobar, the C++ standard states that a new element is created at position 123 and is initialized to zero. That is, in this case, the pair (123, 0) is inserted into the map. I am interested in knowing what the standard says should happen in the
3
1281
by: deancoo | last post by:
I'm hoping this isn't too off topic, but I'm not sure if I've included some of my member functions in the right class. I needed to develop numerous functions to help define object A. These functions currently exist in the class definition for object A. The thing is, there is another object (B) who's job it is to call these functions when the app starts up, defining all possibilities for object A's, then allow future object A's to look up...
13
2075
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e. containing garbage), or being default initialised (i.e. containing the default value for that type). Here are some examples of the former: struct MyStruct {
16
1477
by: silversurfer2025 | last post by:
Hello everyone, once again, I have a very basic problem in C++, which I was not able to solve (maybe because of my Java-Experience or just because it is always the small syntax-things which break my neck in C++)... I would like to have a static variable in class A and change its value from class B (they are not related to each other) For example:
14
2581
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
2
1901
by: .rhavin grobert | last post by:
i have (do try to have?) the following... & = breakpoints in debugger // ---------------------------------------------------------------- // cx.h class CX { public: CX(CX* pcx = NULL); virtual ~CX();
13
1653
by: Anarki | last post by:
#include <iostream> using namespace std; class ConstantMember { const int m_const; public: ConstantMember(int cons = 10):m_const(cons){} int getConst() const{ return m_const;} };
0
9423
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
10212
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
10047
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...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7410
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
6674
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
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
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.