我有一个基类A和派生类B.
B类是从A公开的
如果A是类a是成员变量,我想访问成员变量的地址
当我使用受保护和公共访问说明符时,我正在观察不同的行为.
在这种情况下,当A类成员a 受到保护时,我得到:
cout<<&A::a << endl; 给我一个编译器错误..
但是cout<<&(A::a)<<endl;有效并给我正确的结果.
在这种情况下,当A级成员a 公开时,我得到:
为什么会这样?
这是完整的代码:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
void fun()
{
cout<<"A's fun"<<endl;
}
public:
A(int Aarg):a(Aarg)
{
cout << "A's construction done" <<endl;
}
};
class B: public A
{
protected:
int b;
public:
void fun()
{
cout << "B's fun"<<endl;
}
B(int As, int Bs):A(As), b(Bs)
{
std::cout<<"B's Construction Done" <<std::endl;
}
void show() …Run Code Online (Sandbox Code Playgroud) 在进行编程时,我使用的是assert以及NULL指针验证.
但正如我所知,断言仅在DEBUG模式下有用.
我的问题是假设我有一个内部指针,我肯定不能是NULL示例函数返回一个指针(但指针不是类的成员)在这种情况下,我可以使用断言
test* ptr = fun(); // return a pointer of type test
assert(ptr);
//do some operation
Run Code Online (Sandbox Code Playgroud)
或NULL指针验证
test* ptr = fun(); // return a pointer of type test
assert(ptr);
if (NULL != ptr)
{
//do some operation
}
Run Code Online (Sandbox Code Playgroud)
这里的代码实践很好.据我所知,它将是第二个.因为我遇到了一些ptr的值由于某些我们甚至无法想到的异常情况而返回NULL的情况.
但我们还有其他更好的选择吗?
我有一个shell(ksh)脚本.我想确定某个目录是否存在/tmp,如果存在,那么我必须将其删除.我的脚本是:
测试
#!/usr/bin/ksh
# what should I write here?
if [[ -f /tmp/dir.lock ]]; then
echo "Removing Lock"
rm -rf /tmp/dir.lock
fi
Run Code Online (Sandbox Code Playgroud)
我该怎么办?我没有得到想要的结果:当我执行脚本并且我没有Removing Lock在屏幕上输出时,目录没有被删除.
我手动检查并且锁定文件存在于该位置.锁定文件set MUTEX_LOCK "/tmp/dir.lock"由TCL程序创建.
我刚刚创建了一个带有整数变量和指针变量的类.创建对象后,我将其传递给函数.即使在返回函数之后,程序也没有抛出异常
#include"iostream"
using namespace std;
class A
{
public :
int i;
char *c;
void show();
};
void func(A obj);
int main()
{
A a;
a.i = 10;
a.c = "string";
cout << " Before Fun " << endl;
a.show();
cout << " Going To Call func " << endl;
func(a);
cout << " After func " << endl;
a.show();
return 0;
}
void A::show()
{
cout << " The valuses in Object are " << i << '\t' << …Run Code Online (Sandbox Code Playgroud) 是int一个class?
请考虑下面的代码
#include"iostream"
using namespace std;
class test{
public:
int a;
test(int x)
{
cout<<"In test Constructor"<<endl;
a = x;
}
};
int main()
{
test *obj = new test(10);// Constructor get called
int *x = new int(10); // Expecting the same if "int" is a class
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有两个整数数组
int A[] = {2, 4, 3, 5, 6, 7};
int B[] = {9, 2, 7, 6};
Run Code Online (Sandbox Code Playgroud)
而且我必须得到这些数组的交集.
即输出为-2,6,7
我想通过在数据结构中保存数组A然后我想要比较所有元素直到大小A或B然后我将得到交集.
现在我有一个问题,我需要首先将数组A的元素存储在容器中.
我会跟着像 -
int size = sizeof(A)/sizeof(int);
Run Code Online (Sandbox Code Playgroud)
为了获得大小,但通过这样做,我将获得大小后,我想访问所有元素也存储在容器中.
在这里,我用来找到交叉点的代码 - >
#include"iostream"
using namespace std;
int A[] = {2, 4, 3, 5, 6, 7};
int B[] = {9, 2, 7, 6};
int main()
{
int sizeA = sizeof(A)/sizeof(int);
int sizeB = sizeof(B)/sizeof(int);
int big = (sizeA > sizeB) ? sizeA : sizeB;
int small = (sizeA > sizeB) ? sizeB …Run Code Online (Sandbox Code Playgroud) 我在第30行遇到错误(const Date date2 = new Date(2012年12月31日);)
错误消息是:从'Date*'转换为请求的非标量类型'const Date'
以下是我的源代码:
class Date
{
private :
int day ;
int month ;
int year ;
public :
Date(){
day = 1;
month = 1;
year = 2000;
}
Date(int d, int m, int y) : day(d), month(m), year(y){
}
int getDay () const { return day ;}
int getMonth () const { return month ;}
int getYear () const { return year ;}
};
int main ()
{
const Date date ; …Run Code Online (Sandbox Code Playgroud)