小编Ast*_*mit的帖子

在派生类中访问成员变量的地址.当成员具有不同的访问说明符时,行为会发生变化

我有一个基类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)

c++ public visual-c++ visual-c++-2010 c++11

11
推荐指数
1
解决办法
1322
查看次数

使用Assert和NULL指针验证哪个更好用

在进行编程时,我使用的是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的情况.

但我们还有其他更好的选择吗?

c++ assert visual-c++

6
推荐指数
2
解决办法
1万
查看次数

仅当目录使用shell脚本时才删除目录

我有一个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程序创建.

shell

5
推荐指数
1
解决办法
2万
查看次数

如何证明Copy Constructor是必需的

我刚刚创建了一个带有整数变量和指针变量的类.创建对象后,我将其传递给函数.即使在返回函数之后,程序也没有抛出异常

#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)

c++

3
推荐指数
1
解决办法
270
查看次数

int(内置数据类型)是C++中的类吗?

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)

c++ class

2
推荐指数
1
解决办法
2万
查看次数

如何获得两个数组的交集

我有两个整数数组

    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)

c++ arrays algorithm

1
推荐指数
1
解决办法
5254
查看次数

C++在构造函数中定义const

我在第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)

c++ constructor const

0
推荐指数
1
解决办法
158
查看次数