Copy Constructor Question

Hi All-

I have a very simple program that is stumping me. I was hoping one of you genius' could point me in the right direction. When I run the below, I get the return:

someValue.m_value: 75
this->m_value: 75

I am not expecting this output! My understanding is that when the copy constructor is called "this" should not contain any values! It is almost as if this has already had member-wise shallow copying done, but I've no idea where that would have occurred. Any help would be greatly appreciated!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include <iostream>

class MyClass
{
public:
	int m_value;
public:
	MyClass(int val = 0) :m_value(val)
	{

	}

	MyClass(const MyClass &someValue)
	{
		std::cout << "someValue.m_value: " << someValue.m_value << std::endl;
		std::cout << "this->m_value: " << this->m_value  << std::endl;
	}
};

void someFunction(MyClass xyz)
{
	
}

int main()
{
	MyClass c(75);
	someFunction(c);

    return 0;
}


I am using Visual Studio 2017. I notice this output only on x86!
Last edited on
The value of this->m_value at line 17 is undefined. It could have any value, including 75, or your program crashing.
Registered users can post here. Sign in or register to post.