public class Module
{
// required instance variables
private String codeModule, titleModule;
private int pointsModule;
//three-argument constructor receiving a code, a title string, and a
//number of points,
//which sets the code, title and points data of the created object to
//the received values.
public Module(String aCode, String aTitle, int aPoints)
{
codeModule = aCode;
titleModule = aTitle;
pointsModule = aPoints;
}
//set the instance data value codeModule to received argument newCode.
public void setaCode(String newCode)
{
codeModule = newCode; …Run Code Online (Sandbox Code Playgroud) 我一直认为cstdlib中的随机函数只是rand和srand,但是下面的工作(在Ubuntu 10.10上使用g ++编译)?
我实际上在从Windows移动到Ubuntu时发现了这一点,我的编译失败了,因为它模糊地重载(我已经声明了我自己的'random()'函数).
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
srandom(50);
cout << random();
return 0;
};
Run Code Online (Sandbox Code Playgroud)
此外,下面在Ubuntu上正确编译,在检查stdlib.h之后出现,其中,random()和srandom()等未在std命名空间中声明.这使得它成为一个完全痛苦的屁股......
#include <iostream>
#include <cstdlib>
int main() {
std::cout << random();
return 0;
};
Run Code Online (Sandbox Code Playgroud) 逐字逐句:
以下哪种功能类型不能重载?
- 结构的构造函数
- 类的构造函数
- 类的析构函数
- 类的任何常量方法
- 类的任何静态方法
在堆栈溢出上已经有一些类似于此的问题,但似乎没有什么能直接回答我的问题.如果我重新发布,我会道歉.
我想重载一些模板化类的方法(带有2个模板参数),并对这些方法进行部分模板特化.我无法弄清楚正确的语法,并开始认为这是不可能的.我以为我会在这里发帖,看看能不能得到确认.
要遵循的示例代码:
template <typename T, typename U>
class Test
{
public:
void Set( T t, U u );
T m_T;
U m_U;
};
// Fully templated method that should be used most of the time
template <typename T, typename U>
inline void Test<T,U>::Set( T t, U u )
{
m_T=t;
m_U=u;
}
// Partial specialisation that should only be used when U is a float.
// This generates compile errors
template <typename T>
inline void Test<T,float>::Set( T t, float …Run Code Online (Sandbox Code Playgroud) 所以我在基础编程II课程.我们必须创建一个程序,使4个不同的函数改变运算符的工作方式.我已经查找了多个显示如何执行此操作的示例和文本集,但我无法确定任何代码的含义.对我来说这样的事情应该有效.
int operator++()
{
variableA--;
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这说如果你遇到一个++,那么 - 从变量,现在很明显它不会像这样工作.我发现的所有示例都创建了自己的数据类型.有没有办法使用a int或a 来超载运算符double?
我正在重载方法,因此可以将其分配给事件.必须有更好的方法来做到这一点.
myMethod(){
//Does some stuff...
}
myMethod(object sender, FormClosingEventArgs e){
myMethod();
}
Form.FormClosing += new FormClosingEventHandler(myMethod);
Run Code Online (Sandbox Code Playgroud) 我的主题问题有点误导,我不想像std :: vector那样实现整个类,但我希望能够创建一个名为Container的类,所以我可以像这样声明它:
Container <unsigned int> c;
Run Code Online (Sandbox Code Playgroud)
这是我如何重载<>运算符...
class Container
{
private:
Container()
{
...
}
public:
void operator <>( unsigned int )
{
// what do I put here in the code?
// maybe I call the private constructor...
Container();
}
};
Run Code Online (Sandbox Code Playgroud) #include<iostream>
using namespace std;
class complex {
double real;
double image;
public:
complex(double r=0,double i=0) : real(r), image(i) { };
complex(const complex& c) : real(c.real), image(c.image) { };
~complex(){};
double re() const {
return real;
};
double im() const{
return image;
};
const complex& operator =(const complex&c)
{
real = c.real;
image = c.image;
return *this;
};
const complex& operator +=(const complex&c)
{
real += c.real;
image += c.image;
return *this;
};
const complex& operator -=(const complex&c)
{
real -= …Run Code Online (Sandbox Code Playgroud) 我一直在做这个学校作业.赋值告诉我们创建一个让它的输出运算符(<<)重载的对象.这是我的代码:
#include <ostream>
using namespace std;
template <class T>
class CustomObject {
string print() {
string text = "";
for (int i = 0; i < num_items(); i++) {
text += queue[i];
text += " | \n";
}
return text;
}
friend std::ostream& operator <<(std::ostream &output, CustomObject &q) {
output << "" << q.print();
return output;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我像这样实例化这个对象:
CustomObject<int> co();
Run Code Online (Sandbox Code Playgroud)
并调用其输出方法:
std::cout << co();
Run Code Online (Sandbox Code Playgroud)
哪个将不可避免地调用print方法,并将字符串返回到默认输出流.
但是,我的控制台/调试器中没有可见的输出.
我在这里错过了什么?
PS这不是完整的类,它是通用的,因为其他几种方法和功能无需在此处显示.
PPS num_items()和队列变量是所述休息的一部分,这个类是PriorityQueue对象.因此,queue是指定类型的数组(因此是泛型声明),num_items()只返回数组的计数.
overloading ×10
c++ ×8
ambiguous ×1
c# ×1
events ×1
generics ×1
java ×1
overriding ×1
polymorphism ×1
random ×1
templates ×1