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
}
Related
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
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 6 years ago.
Improve this question
class SomeClass {
int someNum;
const int someConst;
public:
SomeClass() : someNum(12), someConst(15)
{
}
SomeClass operator+(int num) {
SomeClass newSomeClass;
newSomeClass.someNum += num;
return newSomeClass;
}
};
int main() {
SomeClass someClass;
SomeClass newClass;
newClass = someClass + 3;
}
I don't understand why the above doesn't compile, but does so when the const related code is removed.
The problem is here:
SomeClass newClass;
newClass = someClass + 3;
With the first instruction you create the object newClass and the member variable is initialized:
[...] someNum(12), someConst(15) [...]
With the second instruction you're trying to assign a new object constructed by the operator+ to the object.
So actually you're trying to modify the object itself which has a const variable member.
C++ provides a default assignment operator:
newClass = someClass + 3;
means "copy all variable mambers in the object on the right side of the = in the object in the left side".
In that case the variable someConst is declared as constant value, so you cannot overwrite its value. Indeed the operation make a compile error.
Solution
In order to handle this "problem" you need to write a custom assignment operator.
That's an example:
class SomeClass {
int someNum;
const int someConst;
public:
SomeClass() : someNum(12), someConst(15)
{
}
SomeClass operator+(int num) {
SomeClass newSomeClass;
newSomeClass.someNum += num;
return newSomeClass;
}
// Custom assignment operator
SomeClass& operator=(const SomeClass& oth) {
if (this != &oth) {
this->someNum = oth.someNum;
}
return *this;
}
};
In that way you're telling to copy from an object just the member variable someNum and to avoid the cost member.
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:
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
}
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
The C++ draft says that
A non-type template-parameter shall have one of the following
(optionally cv-qualified) types: — integral or enumeration type, —
pointer to object or pointer to function, — lvalue reference to object
or lvalue reference to function, — pointer to member, —
std::nullptr_t.
In the following code I have a pointer to member passed as parameter to a template
using namespace std;
class MyClass
{
public:
int membervar;
};
template< int (MyClass::*var) > struct A
{
// What am I supposed to do with *var? There isn't an object instance to use it!
};
int main(int argc, char *argv[])
{
struct A <&MyClass::membervar> object;
}
The above code compiles (MSVC2012) without errors
The question is: I don't see what am I supposed to do with such a pointer, there isn't an object instance to use it
The only way I can think to answer this is to construct an example:
template< int (MyClass::*var) > struct A
{
A() {
MyClass myclass;
myclass.*var = 42;
cout << myclass.*var << "\n";
}
};
You use it as a regular pointer-to-member:
template< int (MyClass::*var) > struct A
{
void foo()
{
MyClass Blah;
Blah.*var = 3;
}
};
Nothing prevents A from having methods taking MyClass as parameters though:
template <int (MyClass::*var)>
struct A {
int get(MyClass const& mc) const { return mc.*var; }
void set(MyClass& mc, int i) { mc.*var = i; }
};
int main() {
MyClass mc;
A<&MyClass::membervar> a;
a.set(mc, 3);
std::cout << a.get(mc) << "\n"; // prints 3
}