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 21 2020
> Anyway here comes my question:
class Chocolate { public:
int a;
double b;
};
int main() { Chocolate choco; }
Do "a" and "b" get initialized to zero?
No, the compiler generated default ctor leaves scalar types uninitalised.
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?
Again no. The default ctor you wrote is identical to the compiler
generated default ctor, it leaves a and b uninitialised.
You can write this
Chocolate choco = Chocolate();
which would zero initalise a and b in the first definition of Chocolate
you gave but not in the second. Chocolate() is value initialsation which
differs from default initialisation in that it zero initialises scalar
types in a class without a user declared ctor.
john
JKop wrote: 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).
The compiler won't generate a default constructor for you if you write a
user defined one, if that's what you mean.
2) operator= and the copy constructor will change for every extra member variable that's added to the class.
They just do a memberwise copy. So yes, you could say that they change
when you add members to your 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);
Yes.
And then is the SomeClass(const SomeClass&) copy constructor called?
Yes.
Or... does the compiler simply look for a different copy constructor, eg. SomeClass(const double&)?
That's not a copy constructor, but a conversion constructor. And yes,
that one is needed, two. First, a nameless temporary object of class
SomeClass is created using that constructor, then k is copy-constucted
from that one. Note however, that the C++ standard gives explicit
permission to directly create k from the double value, omiting the
temporary, but even if the copy constructor is never called, it still
has to be available. If, however, you write:
SomeClass k(78.222);
only the conversion constructor from double is needed and the copy
constructor is not involved.
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.
Yes. This constructor-style initialization can come in handy in
templates.
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.
Well, the above _is_ a function declaration.
Is the only way to get around this to write:
int j(0);
?
No. You can do:
int j = int();
But usually, I'd just write the more intuitive:
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?
No. They stay uninitialized.
And if so, what does it? Is it the miranda default constructor?
It's the (empty) compiler-generated 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?
You didn't write any code to initialze a or b, so they don't get
initialized.
#include <string>
class Cheese
{
public:
int a;
double b;
std::string name;
};
int main()
{
Cheese mouse;
}
So in the above, "a" and "b" contain white noise, while "name" has been
constructed properly.
Is there any way I can turn the function declaration into an object
definition?!
I see that I can do:
Cheese mouse = Cheese();
Is that the *only* way?
So anyway...
I see in a lot of Win32 documentation that when they have a structure that's
to be passed to a Win32 function, like:
struct SETTINGS
{
int AMOUNT_PORTS;
bool ANY_OPEN;
unsigned char* SET_ID;
};
that in the code they have:
SETTINGS sets;
memset(&sets,0, sizeof(sets));
So instead of that can one write:
SETTINGS sets = SETTINGS();
Another thing with my way, any pointer variables are given they're proper
null pointer value, as opposed to just 0.
BTW, I've also just noticed lately that you can do the following:
extern void Blah(unsigned long);
extern void Blah(unsigned);
extern void Blah(unsigned short);
int main()
{
Blah(unsigned short(52U));
}
Happy Days!
-JKop
In the following, are "Dummy" and "DummyClone " identical?
#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.name;
k = original.k;
}
DummyClone& operator=(const DummyClone& lhs)
{
a = lhs.a;
name = lhs.name;
k = lhs.k;
}
~DummyClone() { }
};
int main()
{
Dummy x;
DummyClone y;
}
Have I left out any miranda functions? Are there 6 in all?
-JKop
* JKop:
I see in a lot of Win32 documentation that when they have a structure that's to be passed to a Win32 function, like:
struct SETTINGS { int AMOUNT_PORTS; bool ANY_OPEN; unsigned char* SET_ID; };
that in the code they have:
SETTINGS sets; memset(&sets,0, sizeof(sets));
So instead of that can one write:
SETTINGS sets = SETTINGS();
In theory yes, in practice no, because many compilers do not respect
the standard in this regard.
Instead write
SETTINGS sets = {0};
Often the following idiom is useful:
SOME_MS_UPPERCA SE_TYPE ugh = {sizeof(ugh)};
STATIC_ASSERT( offsetof( SOME_MS_UPPERCA SE_TYPE, hnSize ) == 0 );
You can find a nice implementation of STATIC_ASSERT in e.g. the
Boost library, <url: http://www.boost.org>.
Another thing with my way, any pointer variables are given they're proper null pointer value, as opposed to just 0.
0 is the null value for pointers.
--
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?
"Alf P. Steinbach" <al***@start.no > wrote in message
news:41******** *******@news.in dividual.net... Another thing with my way, any pointer variables are given they're
proper null pointer value, as opposed to just 0.
0 is the null value for pointers.
I think he's talking about the difference between "all bits zero" and a null
pointer. While the literal zero can be used to signify a null pointer, that
doesn't mean that a null pointer must have a representation where all of its
bits are zero.
--
David Hilsee
Alf P. Steinbach posted: So instead of that can one write:
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? Another thing with my way, any pointer variables are
given they're proper null pointer value, as opposed to just 0.
0 is the null value for pointers.
Not necessarily. And the Standard provides for this. Where
you write:
int* k = 0;
It replaces 0 with the appropriate null pointer value.
But when you use memset, there's no replacement.
-JKop
* JKop: Alf P. Steinbach posted:
So instead of that can one write:
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.
--
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?
JKop wrote: In the following, are "Dummy" and "DummyClone " identical?
No.
#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.name; k = original.k; }
The compiler generated copy constructor will do a memberwise copy
initializiation , while you do a default (or in the case of built-in
types no) initialization, followed by an assignment. This makes a
difference for the std::string member, which first gets
default-construted and then assigned, instead of copy-constructed. A
copy constructor that does the same as the compiler generated one would
do looks like 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;
}
Have I left out any miranda functions? Are there 6 in all?
-JKop
--
Kyle: "Hey, Stan, now that Terrance & Phillip has been taken off the
air, what
are we gonna do for entertainment?"
Stan: "I don't know. We, we could start breathing gas fumes."
Cartman: "My uncle says that smoking crack is kinda cool"
Kyle: "Hey, why don't we watch some of those porno movie thingies?" 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: 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...
|
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: 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();...
|
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: 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...
| |