目前我的重载operator>>函数接受输入[4: 1 2 3 4 ]并且工作正常。但是我怎样才能忽略任意数量的空格以便它可以接受[ 4 : 1 2 3 4 ],即输入之前的任意数量的空格?
istream& operator>>( istream & stream, my_vector & vector_a ) {
string token2;
int vec_size;
vector<double> temp_vec;
bool push = false;
while (stream >> token2) {
if (token2[0] == '[' && token2[2] ==':') {
push = true;
}
if (token2 == "]") {
break;
}
else if(!(token2[0] == '[' && token2[2] ==':')) {
stream.setstate(ios::badbit);
}
if(push) {
istringstream str(token2);
double v;
if …Run Code Online (Sandbox Code Playgroud) 如何在 C++ 中重载双下标运算符 [][] ?
我尝试了很多方法..没有任何具体的答案..
提前致谢..
我已经尝试过这个..但我知道它不正确
class Matrix {
int row;
int col;
int ** values;
int ptr;
public:
Matrix(const int r, const int c) {
ptr = -1;
row = r;
col = c;
values = new int*[row];
for(int i=0; i<row; i++) {
values[i] = new int[col];
}
}
int & operator[](int p) {
if(ptr == -1)
ptr = p;
return values[ptr][p];
}
};
Run Code Online (Sandbox Code Playgroud) 考虑以下场景:
int myNumber = 10;
string formattedNumber = myNumber.ToString("0000"); // output will be "0010"
Run Code Online (Sandbox Code Playgroud)
即,我们可以ToString()像上面的代码一样格式化一个整数。我有一个名为的类myNewClass,其中覆盖了ToString(),我的目标是使用覆盖的 ToString() 方法获得格式化的输出。
public class myNewClass
{
public int MyProperty { get; set; }
public myNewClass(dynamic x)
{
MyProperty = x;
}
public override string ToString()
{
return MyProperty.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
现在正在创建此类的对象:
myNewClass p = new myNewClass(10);
Run Code Online (Sandbox Code Playgroud)
当我ToString()用格式化调用时,它不会编译:
var m = p.ToString("0000");//gives error that "no overloaded method Tostring() takes one argument."
Run Code Online (Sandbox Code Playgroud)
所以我改变了函数签名如下:
public override string ToString(string format)
{ …Run Code Online (Sandbox Code Playgroud) 在java中,我有2个重载方法,一个是main方法,所以从main方法调用重载方法。
public class Test {
public static void main(String[] args) throws IOException {
doSomething(null);
}
private static void doSomething(Object o) {
System.out.println("method with Object in signature is called.");
}
private static void doSomething(String s) {
System.out.println("method with String in the signature is called.");
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,当我运行此 java 代码时,它将调用 doSomething(String s) 方法并打印
调用签名中带有 String 的方法。
我认为它会调用 doSomething(Object o) 方法,但它不会发生。
那么任何人都可以更详细地向我解释这一点,为什么会发生这种情况以及如何发生?
谢谢你。
哪种方式更有效?this->X 或只是 X 有什么区别吗?我认为它的“无效”版本 bcs 编译器不需要调用构造函数或 smth,只需添加。
void operator+=(vect right)
{
this->x += right.x;
this->y += right.y;
}
void operator+=(vect right)
{
x += right.x;
y += right.y;
}
vect& operator+=(vect right)
{
x += right.x;
y += right.y;
return *this;
}
Run Code Online (Sandbox Code Playgroud) 我需要重载这个运算符(mainMenu 是名为“Menu”的类类型):
if (mainMenu) {
cout << "The mainMenu is valid and usable." << endl;
}
Run Code Online (Sandbox Code Playgroud)
我试过这个,但没有用:
bool operator!(const Menu& lobj);
Run Code Online (Sandbox Code Playgroud) 请解释一下,为什么当我编写 4 个重载方法并调用它时 => 它选择以“int”为默认值的方法,而不是“byte”,后者更接近/更好,因为它可以存储从 -127 到 128 的值?
class Main {
public static void method(short s) {
System.out.println("short");
}
public static void method(byte b) {
System.out.println("byte");
}
public static void method(int i) {
System.out.println("int");
}
public static void method(long l) {
System.out.println("long");
}
public static void main(String[] args) {
// put your code here
method(10);
}
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中:
请告诉我为什么只调用派生类成员函数,是因为hiding吗?以及为什么在 中没有发生错误b.func(1.1);,参数是int x。
#include <bits/stdc++.h>
using namespace std;
class A {
public:
virtual int func(float x)
{
cout << "A";
}
};
class B : public A {
public:
virtual int func(int x)
{
cout << "B";
}
};
int main()
{
B b;
b.func(1); // B
b.func(1.1); // B
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我很想知道如果一个类实现了两个或多个具有相似方法的接口,是否可以将其视为方法重载。如果不是,那么正确的术语是什么?
举个例子
public interface I1 {
int method1(String input);
}
public interface I2 {
void method1(int input);
}
public class C1 implements I1, I2 {
public int method1(String input){ return 0;}
public void method1(int input){}
}
Run Code Online (Sandbox Code Playgroud) 我们有两个班级。
class A{
public:
void fun(){
cout<<"Parent\n";
}
};
class B:public A{
public:
void fun(){
cout<<"Child\n";
}
};
Run Code Online (Sandbox Code Playgroud)
我想弄清楚是否fun()会考虑该功能overloaded或overridden. 我尝试使用override关键字,它说该方法未被覆盖。但是我想知道该override关键字是否仅在父类中的函数被编写为virtual以及在上述情况下该函数是否可以被视为被覆盖时才有效。
另外,我想知道重写方法是否总是意味着后期绑定/运行时多态性?
overloading ×10
c++ ×6
java ×3
function ×2
inheritance ×2
overriding ×2
byte ×1
c# ×1
double ×1
int ×1
interface ×1
oop ×1
optimization ×1