'this'指针究竟存储在内存中的哪个位置?它是在堆栈,堆中还是在数据段中分配的?
#include <iostream>
using namespace std;
class ClassA
{
int a, b;
public:
void add()
{
a = 10;
b = 20;
cout << a << b << endl;
}
};
int main()
{
ClassA obj;
obj.add();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我调用成员函数add(),接收器对象隐式传递为'this'指针.凡被this存储在内存中?
我是C++编程的新手,当我做一些C++程序时,我怀疑这就是为什么当我将一个对象作为参数传递给一个函数时调用复制构造函数的原因.请看我的下面的代码,我将一个类的对象作为参数传递给函数display(),但它调用复制构造函数然后控件正在触及display()函数,但我理解为什么它请帮助.
#include "stdafx.h"
#include <iostream>
using namespace std;
class ClassA
{
private:
int a, b;
public:
ClassA()
{
a = 10, b = 20;
}
ClassA(ClassA &obj)
{
cout << "copy constructor called" << endl;
a = obj.a;
b = obj.b;
}
};
void display(ClassA obj)
{
cout << "Hello World" << endl;
}
int main()
{
ClassA obj;
display(obj);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我是C++编程的新手,在做一些C++程序时我有一个疑问,那就是如何实现静态成员函数的动态绑定.正常成员函数的动态绑定可以通过将成员函数声明为虚拟来实现,但是我们不能将静态成员函数声明为虚函数,所以请帮助我.请看下面的例子:
#include <iostream>
#include <windows.h>
using namespace std;
class ClassA
{
protected :
int width, height;
public:
void set(int x, int y)
{
width = x, height = y;
}
static void print()
{
cout << "base class static function" << endl;
}
virtual int area()
{
return 0;
}
};
class ClassB : public ClassA
{
public:
static void print()
{
cout << "derived class static function" << endl;
}
int area()
{
return (width * height);
}
}; …Run Code Online (Sandbox Code Playgroud) 当我们通过值从方法返回一个对象时调用复制构造函数的原因.请看我的下面的代码,因为我从方法返回一个对象,而返回控件正在击中复制构造函数然后返回.我不理解以下事情:
1)为什么它是调用复制构造函数.
2)哪个对象隐式传递给复制构造函数,
3)对象复制构造函数将复制内容,
4)返回时复制对象内容的必要性.所以请帮忙.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class ClassA
{
int a, b;
public:
ClassA()
{
a = 10;
b = 20;
}
ClassA(ClassA &obj)
{
cout << "copy constructor called" << endl;
}
};
ClassA function (ClassA &str)
{
return str;
}
int main ()
{
ClassA str;
function(str);
//function(str);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么编译器没有为包含常量数据成员的类添加默认构造函数.请看下面的代码,因为我已经声明了常量数据成员'a',并且在尝试为类'ClassA'创建对象时,它表示没有适当的默认构造函数可用.请帮忙.
#include "stdafx.h"
#include <iostream>
using namespace std;
class ClassA
{
private:
const int a;
public :
void print()
{
cout << "hello world" << endl;
}
};
int main()
{
ClassA obj;
obj.print();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图在for循环中将无符号数与带符号数进行比较,但是在for循环后它没有执行语句,这意味着for循环不起作用,我想.我的代码是:
#include <stdio.h>
int main()
{
unsigned int i;
for (i = 8; i >= -1; i--)
printf ("%d\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,printf语句没有被执行,所以我的for循环出了什么问题.我们不能将无符号数与有符号数进行比较吗?
#include <stdio.h>
int main()
{
char a[] = "hello";
char *ptr = a;
printf ("%c\n",*ptr++);//it prints character 'h'.
printf ("%c\n",*ptr);//it prints character 'e'.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
据我所知:在上面的代码中,在*ptr++表达,都*和++具有相同的优先级和操作将需要由右至左的地方,这意味着指针将递增第一和下一个顺从会发生.所以它应该'e'在第一个printf语句中打印字符.但事实并非如此.
所以我的问题是:*ptr++如果它没有在第一个printf语句中取消引用该位置,它将在何处存储递增的值(in,)?
我是新手C++ programming,我在阅读C++有关复制构造函数时遇到了疑问.当我们将类的对象传递给外部函数作为pass by value时,为什么复制构造函数会调用.请按以下方式查看我的代码.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line(); // destructor
private:
int *ptr;
};
// Member functions definitions including constructor
Line::Line(int len)
{
cout << "Normal constructor allocating ptr" << endl;
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "Copy constructor allocating …Run Code Online (Sandbox Code Playgroud) 我是C++编程的新手,我正在阅读继承概念,我对继承概念有疑问,即如果基类和派生类具有相同的数据成员会发生什么.还请仔细阅读我的代码如下:
#include "stdafx.h"
#include <iostream>
using namespace std;
class ClassA
{
protected :
int width, height;
public :
void set_values(int x, int y)
{
width = x;
height = y;
}
};
class ClassB : public ClassA
{
int width, height;
public :
int area()
{
return (width * height);
}
};
int main()
{
ClassB Obj;
Obj.set_values(10, 20);
cout << Obj.area() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在上面我声明了与Base类数据成员同名的数据成员,并且我set_values()使用派生的Class Object 调用该函数来初始化数据成员width和height.
当我调用该area()函数时,为什么它返回一些垃圾值而不是返回正确的值.只有当我声明与派生类中的基类数据成员具有相同名称的数据成员时才会发生这种情况.如果我删除派生类中声明的数据成员,它工作正常.那么派生类中的声明有什么问题?请帮我.