考虑以下两种情况(编辑只是为了完成整个问题并使其更清晰)
案例1 :(如下面正确提到的那样编译)
//B.h
#ifndef B_H
#define B_H
#include "B.h"
class A;
class B {
A obj;
public:
void printA_thruB();
};
#endif
//B.cpp
#include "B.h"
#include <iostream>
void B::printA_thruB(){
obj.printA();
}
//A.h;
#ifndef A_H
#define A_H
#include "A.h"
class A {
int a;
public:
A();
void printA();
};
#endif
//A.cpp
#include "A.h"
#include <iostream>
A::A(){
a=10;
}
void A::printA()
{
std::cout<<"A:"<<a<<std::endl;
}
//main.cpp
#include "B.h"
#include<iostream>
using namespace std;
int main()
{
B obj;
obj.printA_thruB();
}
Run Code Online (Sandbox Code Playgroud)
案例2 :(唯一的修改......没有编译错误)
//B.h
#include …Run Code Online (Sandbox Code Playgroud) 我需要澄清一个问题,为什么我们需要范围解析运算符或this指针来访问模板基类中公开继承的成员.据我所知,这是为了增加清晰度,但是如何this增加任何进一步的清晰度,而不仅仅指出它是该类的成员.
为了使我的问题更清楚,我添加了一些代码.
#include <iostream>
using namespace std;
template <class T, class A>
class mypair {
public:
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
virtual void printA()
{
cout<<"A"<<a<<endl;
cout<<"B"<<b<<endl;
}
};
template <class T, class A>
class next: mypair<T,A>
{
public:
next (T first, T second) : mypair<T,A>(first, second)
{
}
void printA()
{
cout<<"A:"<<mypair<T,A>::a<<endl; // this->a; also works
cout<<"B:"<<mypair<T,A>::b<<endl; // this-b; also works
}
};
int main () {
next<double,float> newobject(100.25, …Run Code Online (Sandbox Code Playgroud) 只是找到模板的方法,所以尝试了一些东西.
让我知道我在这里做错了什么.
我试图重载一个继承的模板虚拟方法.
// class templates
#include <iostream>
using namespace std;
template <class T, class A>
class mypair {
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
virtual A getmax ();
};
template <class T, class A>
A mypair< T, A>::getmax ()
{
A retval;
retval = a>b? a : b;
return retval;
}
template <class T, class A>
class next : public mypair <T, A> {
A getmax ()
{
cout <<" WHOO HOO";
}
}; …Run Code Online (Sandbox Code Playgroud) 尝试了stackeroverflow qn所以它让我思考为什么不重载该函数,我想出了一个稍微不同的代码,但它说该函数不能重载.我的问题是为什么?还是有另一种方式?
#include <iostream>
using std::cout;
class Test {
public:
Test(){ }
int foo (const int) const;
int foo (int );
};
int main ()
{
Test obj;
Test const obj1;
int variable=0;
do{
obj.foo(3); // Call the const function
obj.foo(variable); // Want to make it call the non const function
variable++;
usleep (2000000);
}while(1);
}
int Test::foo(int a)
{
cout<<"NON CONST"<<std::endl;
a++;
return a;
}
int Test::foo (const int a) const
{
cout<<"CONST"<<std::endl;
return a;
}
Run Code Online (Sandbox Code Playgroud) 在企业界,对于多种语言来说,知识渊博(通过知识渊博,我的意思不是专家或新手,但具有一些编码经验)是否更好?
要么
最好是成为一种语言的专家(比如说c ++或java),但只掌握其他语言的基础知识.
我问这个问题,因为我觉得语言可以根据他们提供的功能区分,如垃圾收集等.但这可以用其他语言实现......为什么人们更喜欢一种语言而不是另一种?
这个董事会的一般观点是什么?
c++ ×5
class ×2
inheritance ×2
templates ×2
c# ×1
coding-style ×1
const ×1
function ×1
java ×1
overloading ×1
virtual ×1