小编MWr*_*ght的帖子

通过引用传递时取消引用指针

当通过引用函数传递时取消引用指针会发生什么?

这是一个简单的例子

int& returnSame( int &example ) { return example; }

int main()
{
  int inum = 3;
  int *pinum = & inum;

  std::cout << "inum: " <<  returnSame(*pinum) << std::endl;

  return 0;          

}
Run Code Online (Sandbox Code Playgroud)

是否有临时对象产生?

c++ pointers reference

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

C++ std :: shared_ptr用法和信息

我想std::shared_ptr在我的代码中使用.我已经看到有关于这个问题的其他问题,但我仍然遇到编译器错误.我有正确版本的gcc和设置吗?

我做了什么:

我曾试图单独编译我既头码- <memory><tr1/memory>,但仍然在这两种情况下得到下面的错误.

我正在使用的gcc版本是

gcc --version
gcc (GCC) 4.3.2
Run Code Online (Sandbox Code Playgroud)

当我包含<memory>标题我使用std::shared_ptr和我使用的<tr1/memory>标题std::tr1::shared_ptr?它是否正确?

我已经设置了shared_ptr,如下所示:

std::shared_ptr<A*> ptr_A = shared_ptr( new A() );
Run Code Online (Sandbox Code Playgroud)

我得到的错误如下:

src/WH.cxx:156: error: 'shared_ptr' is not a member of 'std'
src/WH.cxx:162: error: 'shared_ptr' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

当我尝试<tr1/memory>标题时

src/WH.cxx:156: error: 'std::tr1' has not been declared
src/WH.cxx:162: error: 'shared_ptr' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

看起来我没有正确包含一些东西.有任何想法吗?

我知道boost库有,shared_ptr但这些库目前不适合我.

编辑:只是添加,我的编译器选项如下:g ++ -O3 -g3 -m32 -fPIC …

c++ g++ shared-ptr

18
推荐指数
3
解决办法
3万
查看次数

Log4j.xml类别多个appender-ref具有不同的优先级

我希望能够拥有不同优先级的不同appender

<category name="my.package" additivity="false">  
    <priority value="fatal" />  
    <appender-ref ref="consoleAppender" />
    <appender-ref ref="fileAppender" />
</category>
Run Code Online (Sandbox Code Playgroud)

我认为这样的东西会奏效,但事实并非如此

<category name="my.package" additivity="false">  
    <priority value="fatal" ref="consoleAppender" /> 
    <priority value="info" ref="fileAppender"/> 
    <appender-ref ref="consoleAppender" />
    <appender-ref ref="fileAppender" />
</category>  
Run Code Online (Sandbox Code Playgroud)

java log4j

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

C,C++与Python的接口

我的c ++代码已成长为指数级.我有很多变量(大多数是布尔值),每次运行我的代码时都需要更改(不同的运行条件).我使用main( int argc, char* argv[])过去的函数的参数命令行输入完成了这个.

由于这种方法已经成为累赘(I有18种不同的运行条件下,因此,18个不同的参数:-(),我想移动到与Python接口(如果需要的话击).理想情况下,我想编码Python脚本,其中我设置数据成员的值,然后运行代码.

有没有人有任何可以帮助我的指针/信息?更好的是我可以查找一个简单的编码示例或URL.

从原始问题编辑:

对不起,我不认为我对我的问题很清楚.我不想main( int argc, char* argv[])在c ++中使用该功能.而不是在命令行上设置变量.我可以使用python来声明和初始化我的c ++代码中的数据成员吗?

再次感谢迈克

c c++ python bash

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

C++在类中使用类

我有一个困扰我的基本问题.

在类中使用类时,我可以在头文件中定义要使用的类的标题.我已经看到了两种方法,并想知道这两种方法之间的区别?

EX1

#include "ClassA.h"

class ClassB {

   public:
     ClassB();
     ~ClassB();
     ClassA* a;
};
#endif
Run Code Online (Sandbox Code Playgroud)

ex2这是另一种方法.ClassA标头将在ClassB源文件中定义.

class ClassA;

class ClassB {

   public:
     ClassB();
     ~ClassB();
     ClassA* a;
};
#endif
Run Code Online (Sandbox Code Playgroud)

这两种方法有什么不同?

c++

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

一起添加两张地图

我想添加两个地图以及以下行为.

如果键存在 - >将两个键值一起添加.

如果键不存在 - >插入对映射.

我已经看了一些标准库算法.即改造,但似乎没有做我想要的.

取自此LINK

template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op )
{
  while (first1 != last1)
    *result++ = op(*first1++);  // or: *result++=binary_op(*first1++,*first2++);
  return result;
}
Run Code Online (Sandbox Code Playgroud)

我的想法来自于我在第二张地图中使用时只有一个迭代器和相应的仿函数

 *result++=binary_op(*first1++,*first2++);
Run Code Online (Sandbox Code Playgroud)

因此,我将无法遍历我的第二个地图以找到键值.

一种想法只是让我自己的算法有一个微妙的变化.

template < class InputIterator, class ContainerType, class BinaryOperator >
  void myTransform ( InputIterator first1, InputIterator last1,
                               ContainerType cont2,  
                               BinaryOperator binary_op )
{
  while (first1 != last1)
    binary_op(first1++, cont2); //cont2 passed by …
Run Code Online (Sandbox Code Playgroud)

c++ ubuntu gcc gnu

4
推荐指数
2
解决办法
3376
查看次数

Mock ProceedingJoinPoint签名

我试图模拟ProceedingJoinPoint类,我很难嘲笑一个方法.

以下是调用mock类的代码:

...
// ProceedingJoinPoint joinPoint

Object targetObject = joinPoint.getTarget();
try {

  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();

  ...
  ...
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我嘲笑的课堂尝试......

accountService = new AccountService();
ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
when(joinPoint.getTarget()).thenReturn(accountService);
Run Code Online (Sandbox Code Playgroud)

我现在不确定如何模拟签名以获得什么方法?

when(joinPoint.getSignature()).thenReturn(SomeSignature); //???
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

java spring aspectj mocking mockito

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

在通过引用函数传递时解除引用指针时调用的复制构造函数

可能重复:
通过引用传递时取消引用指针

在通过引用函数传递时取消引用指针时是否调用了复制构造函数?

这是一个简单的例子

int& returnSame( int &example ) { return example: }

int main()
{
  int inum = 3;
  int *pinum = & inum;

  std::cout << "pinum: " <<  returnSame(*pinum) << std::endl;

  return 0;          

}
Run Code Online (Sandbox Code Playgroud)

我猜猜发生了什么:

当我们取消引用pinum时,我们希望调用复制构造函数,但由于函数是通过值传递的,所以不会调用它?

如果调用了pinum copy构造函数,那么就会产生一个临时对象,并且会使用它的引用,这将是一个非常糟糕的新闻,以未定义的行为形式......

那么会发生什么......未定义的行为?

c++

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

当推回指向指针向量的指针时,C++获取临时地址

我在代码中收到警告:

警告:取临时的地址

我见过类似的问题,但他们没有回答我的具体问题.

这是我的代码正在做的事情:

vector<A*>* ex_A;

ex_A->push_back( &A());  //I get this warning taking address of temporary
Run Code Online (Sandbox Code Playgroud)

这是未定义的行为吗?

我之前确实有这个,这很好,但我不想担心从堆中删除内存.

vector<A*>* ex_A;

ex_A->push_back( new A());
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释这个警告意味着什么吗?

c++

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

使用dynamic_cast和多态的最佳实践

我有一个设计问题,我不知道如何以最好的方式处理.我希望我的代码能够成为未来的证明,而且仍然不会变得混乱和复杂(极客的困境).

目前我的设计有以下设置

derived class D          derived class E
  ^                          ^
  |                          |
derived abstract class B  ->              derived class C
  ^                                            ^ 
  |                                            |
              Abstract Base class A
Run Code Online (Sandbox Code Playgroud)

B从类不同A,类D和类E从类不同BC从类不同A.

由于这些类不同,我需要使用dynamic_cast运算符.

A* a1 = new class C;
C* c1 = dynamics_cast<C*>(a1);
if(c1){ //Success!!!}
...
Run Code Online (Sandbox Code Playgroud)

如你所见,我会想要使用dynamic_cast很多!我需要尝试找到一种很好的方法来做到这一点,而不是if()额外的语句.

这样做有一个简洁的方法吗?我似乎记得在Scott Meyers的书中看到了一些东西,但是我不在家,一两个星期都无法访问它.

任何参考/示例将非常感激.

此外,如果需要,我可以将类B变成一个接口,这将是一种解决方案

B* d1 = new D;
B* e1 = new E; …
Run Code Online (Sandbox Code Playgroud)

c++ oop polymorphism downcast

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

在将指针指向指针(并将指针传递给类)时使用delete进行C++混淆

很抱歉,如果我的上一个问题非常相似,但我还有另一个查询,说明何时以及是否应该使用delete关键字.

示例代码.

我初始化一个类,它指向指针向量作为参数.这些对象从堆中分配内存.然后我将这些对象传递给我的类构造函数,该构造函数也从堆中分配内存.

这是代码:在main函数中

     //This will also change each loop
     vector<A*>* ex1 = new vector<A*>;
     vector<B*>* ex2 = new vector<B*>;         
     vector<C*>* ex3 = new vector<B*>;

     for(int i =0; i < 10; i++){

       ex1->push_back( new A(i); );
       ex2->push_back( new B(i); );
       ex3->push_back( new C(i); );

     }


     MainClass* ex_main = new MainClass(ex1,ex2,ex3);
Run Code Online (Sandbox Code Playgroud)

在MainClass.cxx中

   MainClass::MainClass(vector<A*>* ex_A, vector<B*>* ex_B, vector<C*>* ex_C): m_ex_A(ex_A), m_ex_B(ex_B), m_ex_C(ex_C) {}
Run Code Online (Sandbox Code Playgroud)

在MainClass.h中

   vector<A*>* m_ex_A;
   vector<B*>* m_ex_B;         
   vector<C*>* m_ex_C;
Run Code Online (Sandbox Code Playgroud)

m_ex1,m_ex2和m_ex3已分配给堆栈,但已分配给指针向量的指针.

问题.

我是否删除了类MainClass的析构函数中的m_ex_A,B ans C(我知道我还需要删除每个向量中的元素)以及指向main函数中指针向量的指针吗?

在MainClass的析构函数中的ex(我做了一些比这更通用的东西,但这更快地显示)

for(int i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c++

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

继承和虚拟成员函数

我不是100%肯定我当前设置中会出现的结果.

这可能是一个愚蠢的问题,但我看不到类似的例子.

这是示例代码

Class A {

  public:
    virtual Steer* getSomeParticularInfo(){ return m_steer; }

  private:
    Steer* m_steer:
}


Class B: A {

  public:
    virtual Steer* getSomeParticularInfo(){ return m_steer; }

  private:
    Steer* m_steer:
}

Class C: B {

  public:
    //Does not have its own getSomeParticularInfo() member function

  private:
    Steer* m_steer:
}
Run Code Online (Sandbox Code Playgroud)

我的问题:

如果我调用getSomeParticularInfo.它会来自B类,因为它是最新的派生类还是来自基类?

//Inside Class C constructor
C::C(){
  m_steer = getSomeParticularInfo();
}
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×10

java ×2

aspectj ×1

bash ×1

c ×1

downcast ×1

g++ ×1

gcc ×1

gnu ×1

log4j ×1

mocking ×1

mockito ×1

oop ×1

pointers ×1

polymorphism ×1

python ×1

reference ×1

shared-ptr ×1

spring ×1

ubuntu ×1