Is this them all?:
class Dummy
{
public:
Dummy() {}
Dummy(const Dummy& original) { }
Dummy& operator=(const Dummy& lhs) {}
const Dummy * operator&() const { return this; }
Dummy * operator&() { return this; }
~Dummy() {}
};
Also could anyone please inform me of circumstances in which these are
edited and in which they disappear? For example I'm already aware of the
following:
1) Dummy() disappears if you supply another one that takes arguments, eg.
Dummy(int).
2) operator= and the copy constructor will change for every extra member
variable that's added to the class.
--
Now, take the following:
int main()
{
int j;
}
I'm fully aware that j contains no particular value, it hasn't been
initialized.
Now take the following:
int main()
{
int j = 45;
}
I'm fully aware that that's equal to:
int j = int(45);
But, where you have user-defined types, what exactly goes on?
For instance:
SomeClass k = 78.222;
Does that become:
SomeClass k = SomeClass(78.22 2);
And then is the SomeClass(const SomeClass&) copy constructor called? Or...
does the compiler simply look for a different copy constructor, eg.
SomeClass(const double&)? Or is that even valid?
Anyway here's what I'm getting at: I know int() is equal to zero, ie. if you
specify brackets then it gets intialized to zero. Now ofcourse you have the
problem of:
int main()
{
int j();
}
The compiler doesn't know if it's an object definition or a function
declaration (...if only "extern" was compulsory). As such, it assumes a
function declaration.
Is the only way to get around this to write:
int j(0);
?
Anyway here comes my question:
class Chocolate
{
public:
int a;
double b;
};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero? And if so, what does it? Is it the
miranda default constructor? What about in the following code:
class Chocolate
{
public:
int a;
double b;
Chocolate() {}
};
int main()
{
Chocolate choco;
}
Do "a" and "b" get initialized to zero in the above?
-JKop
Jul 22 '05
21 2025 >> SETTINGS sets = SETTINGS();
> In theory yes, in practice no, because many compilers do not > respect the standard in this regard.
Please explain what you mean - are you saying that not many compilers will compile the above as specified by the Standard?
Yes.
What do they do instead?
In the absence of optimization, here's what should happen:
1) A SETTINGS object is created, all values intialized to 0.
2) sets is copy-constructed from the previous object.
3) The original object is destroyed.
And with optimization:
1) sets is created, all values initialized to 0.
Where do these other alleged C++ compilers go wayward?
-JKop
Thanks Rolf.
So now we have:
#include <string>
class Dummy
{
private:
int a;
protected:
std::string name;
public:
double k;
};
class DummyClone
{
private:
int a;
protected:
std::string name;
public:
double k;
public:
DummyClone() {}
DummyClone* operator&() { return this; }
const DummyClone* operator&() const { return this; }
DummyClone(cons t DummyClone& original)
: a(original.a), name(original.n ame), k(original.k) {}
DummyClone& operator=(const DummyClone& lhs)
{
a = lhs.a;
name = lhs.name;
k = lhs.k;
}
~DummyClone() { }
};
int main()
{
Dummy x;
DummyClone y;
}
Is that it now?
-JKop
* JKop: >> SETTINGS sets = SETTINGS(); In theory yes, in practice no, because many compilers do not > respect the standard in this regard.
Please explain what you mean - are you saying that not many compilers will compile the above as specified by the Standard?
Yes.
What do they do instead?
In the absence of optimization, here's what should happen:
1) A SETTINGS object is created, all values intialized to 0. 2) sets is copy-constructed from the previous object. 3) The original object is destroyed.
And with optimization:
1) sets is created, all values initialized to 0.
Where do these other alleged C++ compilers go wayward?
According to my notes Visual C++ 7.0 is one such alleged compiler... ;-)
Visual C++ 7.1 seems to do it right.
--
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?
On Sun, 01 Aug 2004 20:07:28 GMT, JKop <NU**@NULL.NULL > wrote: Thanks Rolf.
So now we have:
#include <string>
class Dummy { private:
int a; protected:
std::string name; public:
double k; };
class DummyClone { private:
int a; protected:
std::string name; public:
double k;
public:
DummyClone() {} DummyClone* operator&() { return this; } const DummyClone* operator&() const { return this; } DummyClone(cons t DummyClone& original) : a(original.a), name(original.n ame), k(original.k) {} DummyClone& operator=(const DummyClone& lhs) { a = lhs.a; name = lhs.name; k = lhs.k; }
~DummyClone() { } };
int main() { Dummy x; DummyClone y; }
Is that it now?
-JKop
Dummy and DummyClone still differ if value initialised
Dummy* d = new Dummy();
DummyClone* dc = new DummyClone();
Here d->a and d->k are guaranteed to be zero (assuming you have a compiler
which implements value initialisation correctly) but dc->a and dc->k are
uninitialised.
john
On Sun, 01 Aug 2004 20:04:46 GMT, JKop <NU**@NULL.NULL > wrote: >> SETTINGS sets = SETTINGS(); In theory yes, in practice no, because many compilers do not > respect the standard in this regard.
Please explain what you mean - are you saying that not many compilers will compile the above as specified by the Standard?
Yes.
What do they do instead?
In the absence of optimization, here's what should happen:
1) A SETTINGS object is created, all values intialized to 0. 2) sets is copy-constructed from the previous object. 3) The original object is destroyed.
And with optimization:
1) sets is created, all values initialized to 0.
Where do these other alleged C++ compilers go wayward?
-JKop
I think many would fail to iniitalise to zero. As I understand it this was
acceptable pre-2003 standard.
john
> Dummy and DummyClone still differ if value initialised Dummy* d = new Dummy(); DummyClone* dc = new DummyClone();
Here d->a and d->k are guaranteed to be zero (assuming you have a compiler which implements value initialisation correctly) but dc->a and dc->k are uninitialised.
john
And there's no way around this?
Also I presume that if you leave it like:
Dummy* d = new Dummy;
the it contains white noise, yes?
Now here's a tricky one:
A class that has only public data, but it has a contructor:
class Chunky
{
public:
int a;
double b;
char c;
Chunky() {}
};
If I do:
Chunky chunks;
Then "a", "b" and "c" contain white noise. That right?
While if I do:
Chunky chunks = Chunky();
Then it still contains white noise (on account of the constructor). That
right?
What I'm getting at is if a certain class has a contructor, are:
Chunk chunks;
Chunky chunks = Chunky();
identical, in that they both contain white noise?
-JKop
"JKop" <NU**@NULL.NULL > wrote in message
news:PI******** **********@news .indigo.ie... Dummy and DummyClone still differ if value initialised
Dummy* d = new Dummy(); DummyClone* dc = new DummyClone();
Here d->a and d->k are guaranteed to be zero (assuming you have a compiler which implements value initialisation correctly) but dc->a and dc->k are uninitialised.
john
And there's no way around this?
I don't think so.
Also I presume that if you leave it like:
Dummy* d = new Dummy;
the it contains white noise, yes?
Yes. Now here's a tricky one:
A class that has only public data, but it has a contructor:
class Chunky { public: int a; double b; char c;
Chunky() {} };
If I do:
Chunky chunks;
Then "a", "b" and "c" contain white noise. That right?
Right. While if I do:
Chunky chunks = Chunky();
Then it still contains white noise (on account of the constructor). That right?
Right, value initialisation only makes a difference (as far as I can tell)
for types without a constructor. What I'm getting at is if a certain class has a contructor, are:
Chunk chunks; Chunky chunks = Chunky();
identical, in that they both contain white noise?
It depends on the default constuctors of course, but otherwise both are
identical.
john
Which leads me on to POD types.
For instance, take the following:
#include <string>
class Cow
{
public:
int a;
double b;
std::string c;
};
int main
{
Cow dairy_cow = { 5, 67.2, std::string("Mo o!") };
}
It looks like a type is no longer a POD when the above syntax in "main" is
no longer valid. For instance, take:
class Cow
{
public:
int a;
double b;
std::string c;
private:
char t;
};
int main
{
Cow dairy_cow = { 5, 67.2, std::string("Mo o!") };
}
Now the compiler is telling me that a "Cow" can't be defined like that, and
that you've to use a constructor.
I've found the following things render a class no longer a POD:
a) Putting in private or protected member data
b) Putting in a constructor or copy-constructor
And that the following are no problem, it's still a POD:
a) Putting in member functions
b) Putting in operator member functions
(even if they're private or protected)
It's good to know this stuff!
So now I think I fully understand all the initialization stuff in C++.
There's just one thing that I want to figure out:
SomeClass object = SomeClass();
There has to be some sort of way of turning the following:
SomeClass object();
into an object definition. I've tried:
class SomeClass object();
struct SomeClass object();
But still they're a function declaration. That doesn't make sense to me.
It'd even be compatible with C, because while you would prefix "struct" in
C, you still wouldn't put in parenthesis. And there was no "class" in C
either.
Anyone know how to tell the compiler it's an object definition?
-JKop
JKop <NU**@NULL.NULL > wrote: In the following, are "Dummy" and "DummyClone " identical?
[some stuff snipped from these definitions] class Dummy { };
class DummyClone { public: DummyClone* operator&() { return this; } const DummyClone* operator&() const { return this; } };
No. DummyClone contains operator& and Dummy does not. You also made
this mistake in your original post.
int main() { Dummy x; DummyClone y;
}
Have I left out any miranda functions? Are there 6 in all?
You included too many. Don't be misled by mistakes in the book
'C++ by Dummies' (which also invented the term 'miranda functions').
If you don't believe it, add these lines to main:
& DummyClone::ope rator &;
& Dummy::operator &;
and note the compiler error.
Old Wolf posted: You included too many. Don't be misled by mistakes in
the book 'C++ by Dummies' (which also invented the term 'miranda
functions'). If you don't believe it, add these lines to main:
& DummyClone::ope rator &; & Dummy::operator &;
and note the compiler error.
Although the following *does* compile:
class Dummy {};
void Blah(Dummy* a) {}
void Blah(const Dummy*) {}
int main()
{
Dummy k = Dummy();
const Dummy j = Dummy();
Blah(&k);
Blah(&j);
}
-JKop This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robert Allan Schwartz |
last post by:
I've heard the phrase "Miranda function" used to refer to the 6 member
functions supplied by the compiler:
default constructor
copy constructor
destructor
operator=
operator& const
operator&
|
by: ypjofficial |
last post by:
Pls look at this code
////////////////////start///////////
class a
{
public:
a(){
cout<<"inside constructor of a"<<endl;
}
int b()
|
by: smreddy |
last post by:
Is it possible to call a C++ function in C program ? if so ?
Regards
Srinath
|
by: cody |
last post by:
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
i heard people say C++ is slower than C but i can't believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is basically the same.
in C there arent the simplest things present like constants, each struct and
enum...
|
by: Gibby Koldenhof |
last post by:
Hiya,
Terrible subject but I haven't got a better term at the moment.
I've been building up my own library of functionality (all nice conforming
ISO C) for over 6 years and decided to adopt a more OO approach to fit my
needs. Altough I used an OO approach previously for some subparts of the
library it became somewhat difficult to maintain all those parts since they
really are the same thing coded for each part (code duplication). So...
| |
by: roberts.noah |
last post by:
I ran across some code that called memset(this, 0, sizeof(*this)) in
the member function of a structure in C++. This just looked wrong to
me so I did some web searching and it does indeed seem like a
questionable thing to do. Looking around further took me to a
newsgroup discussion that said the better approach was to do something
like sv = Struct(). The thing is that both cases seem to bo "ok" so
long as there are no virtual functions...
|
by: red floyd |
last post by:
Someone asked me about the construct below:
class Base {
public:
// all other elements redacted
Base& operator=(const Base&);
};
class Derived : public Base {
public:
|
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it. Like, for instance
the Objective-C method:
+(void)initialize
Which has the following characteristics: It is guaranteed to be run
|
by: Jess |
last post by:
Hello,
If I have a class that has virtual but non-pure declarations, like
class A{
virtual void f();
};
Then is A still an abstract class? Do I have to have "virtual void
f() = 0;" instead? I think declaring a function as "=0" is the same
|
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: 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,...
|
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: 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...
|
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.
| |