This question already has answers here:
Question about constructors & memory leak
(2 answers)
Rule of Three in C++
(3 answers)
Closed 4 months ago.
So I m creating two classes A and B and I want to practice type casting. So in this this example
I m creating 2 variables , 'foo'(class A type) and 'bar'(class B type) in the stack and when casting from B TO A using 'foo = bar;' I m creating new variable in the Heap and p pointer to it I want my variable 'foo' to point to that variable.I m doing that right ?? and how do I check if everything is working fine ( the value of foo has changed to 7 ) you can view the code and tell me more about what's going on and is it possible to let foo point to the pointer or not??
class A {
public:
int a;
A(int x)
{
a = x;
}
};
class B {
public:
int x;
// conversion from A (constructor):
B(int a)
{
x=a;
}
// B (const A& x) {cout<<"conversion done"<<endl;}
// conversion from A (assignment):
B& operator= (const A& c) {this->x =c.a ;return *this;}
// conversion to A (type-cast operator)
operator A() {
cout<<"typecast"<<endl;
A *p = (new A(x));
cout<<p<<endl;
return *p;
}
};
int main ()
{
A foo(5);
cout<<&foo<<endl;
B bar(7);
foo = bar;
cout<<&foo<<endl;
return 0;
} ```
the output:
0x5b73fff75c
typecast
0x201cdfc1580
0x5b73fff75c
Related
This question already has answers here:
Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior?
(7 answers)
Closed 7 years ago.
#include <iostream>
using namespace std;
class B{
public:
int date;
B(){cout<<"B()"<<endl;}
B(int a){date=a;cout<<"B(int a)"<<endl;}
B(const B& b){
date=b.date;
cout<<"int date;"<<endl;
}
B& operator =(const B& b){
date=b.date;
cout<<"operator(const B& b)"<<endl;
return *this;
}
void print(){
std::cout<<date<<std::endl;
}
};
int main(){
B a(1);//use B(int a)
B* b;
B* c;
*b=a;//use operator(const B& b)
*c=*b;//but this one is wrong,why?,I think it also use operator(const B& b)
new (c)B(*b);
return 0;
}
When I use *c=*b it doesn't work, I think it also uses operator(const B& b), but when I use new(c)B(*b) it is ok.
What is the difference between *c=*b and new(c)B(*b) and why is *c=*b wrong?
Your code:
B* b;
B* c;
Now b and c are unitialized, which means that it is unknown to what memory address they are pointing to.
When you do:
*b = a;
It changes the value at that unknown address to the value of a. Which causes undefined behavior.
Then you do:
*c = *b;
Which basically sets value at unknown address c to value of unknown address b which at this point 'might' still be value of a, but it is unknown.
The reason why
new (c)B(*b);
is working, is the dark magic of undefined behavior.
This question already has answers here:
What are the pointer-to-member operators ->* and .* in C++?
(7 answers)
Closed 7 years ago.
Good day,
I've come across this question, but I'm specifically interested in the "object pointed to by member ..." type operators as listed here on Wikipedia.
I have never seen this in the context of actual code, so the concept appears somewhat esoteric to me.
My intuition says they should be used as follows:
struct A
{
int *p;
};
int main()
{
{
A *a = new A();
a->p = new int(0);
// if this did compile, how would it be different from *a->p=5; ??
a->*p = 5;
}
{
A a;
a.p = new int(0);
// if this did compile, how would it be different from *a.p=5; ??
a.*p = 5;
}
return 0;
}
But this doesn't compile because p is undeclared. (See example)
Could anyone please provide a real-world example of the use of operator->* and/or .* in C++?
Those operators are used for pointer-to-member objects. You won't come across them very often. They can be used to specify what function or member data to use for a given algorithm operating on A objects, for instance.
Basic syntax:
#include <iostream>
struct A
{
int i;
int geti() {return i;}
A():i{3}{}
};
int main()
{
{
A a;
int A::*ai_ptr = &A::i; //pointer to member data
std::cout << a.*ai_ptr; //access through p-t-m
}
{
A* a = new A{};
int (A::*ai_func)() = &A::geti; //pointer to member function
std::cout << (a->*ai_func)(); //access through p-t-m-f
}
return 0;
}
The ->* and .* syntax is the "pointer-to-member" operator that can be used to store pointers to members of a very specific object.
Usage example:
class A {
public: int x;
};
int main() {
A obj;
int A::* memberPointer = &A::b; //Pointer to b, which is int, which is member of A
obj.*memberPointer = 42; //Set the value via an object
A *objPtr = &obj;
objPtr->*memberPointer = 21; //Set the value via an object pointer
}
This question already has answers here:
Default arguments in constructor--C++
(4 answers)
Closed 8 years ago.
I would like to use different number of arguments.
class A {
public:
A(int a, int b);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5); // I use only 1 argument and the second one I let default ?
}
Constructors are (a bit special) functions - regular default parameter syntax applies.
class A {
public:
A(int a, int b = default_value);
};
A::A(int a, int b) {
// constructor code
}
int main() {
A a(5);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why is the constructor H1 and H2 getting called? Can I get the reason?
class A
{
public:
int a;
char b;
A()
{
a = 0;
b = '\n';
}
A(int a)
{
cout << "H1";
}
A(char c)
{
cout << "H2";
}
};
int main()
{
int a = 10;
char c = 'h';
A ob1 = a;
ob1 = c;
}
A ob1 = a;
This initialises an object of type A from an int value. That uses the constructor A(int), printing H1.
ob1 = c;
This assigns a char value to the object. Since there isn't a suitable assignment operator A::operator=(char), which could assign the value directly, it does this in two stages:
Create a temporary object of type A from the char value
Assign that to ob1 using the implicit copy-assignment operator.
The first stage initialises the temporary using the constructor A(char), printing H2.
Hence, the output is H1H2.
class A {
public:
int a;
char b;
A() {
a=0;
b='\n';
}
A(int a) {
cout<<"H1";
}
A(char c) {
cout<<"H2";
}
};
int main() {
int a=10;
char c='h';
A ob1=a; // calls A(int a) because a is typeof int
ob1=c; // calls A(char c) because c is typeof char
}
This question already has answers here:
What does A a() mean? [duplicate]
(2 answers)
Closed 9 years ago.
What does this particular piece of code do? To be more precise, what does test tob(); do?
class test {
private:
int a;
int b;
public:
test (int);
test();
};
test::test() {
cout<<"default";
}
test::test (int x=0) {
cout<<"default x=0";
}
int main() {
test tob();
}
I dont know what does test tob(); do, but it isn't giving any compilation errors.
test tob();
This declares a function which return type is test. It does not create an object. It's also know as most vexing parse.
To create a test object:
test tob;
Also, the way you define a function(include constuctor) with default argument is incorrect.
test::test (int x=0) { // incorrect. You should put it in function when it's first declared
cout<<"default x=0";
}
Below code should work:
class test {
int a;
int b;
public:
explicit test (int = 0); // default value goes here
};
test::test (int x) {
cout<<"default x=0";
}
int main() {
test tob; // define tob object
}