对于一个项目,我试图创建一个带有向量函数的向量类。我试图通过重写+=加法赋值来添加一个向量加法函数。由于我不希望+=函数更改要添加的值,所以我希望将该值作为const reference传递。
我的代码:
Vector2D.h
#pragma once
class Vector2D
{
private:
int m_x, m_y;
public:
Vector2D(int x, int y);
int getX();
int getY();
void setX(int x);
void setY(int y);
Vector2D&
在我的main中,这样的东西是如何在Cpp中初始化的:Testing test
class Testing
{
public:
Testing();
void initalize();
~Testing();
void run();
private:
int x;
int y;
int z;
bool isBugged;
OtherClass otherClass_;
};
顺序是什么?
我正在尝试执行以下代码:
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(int i = 25) { x = i; }
int &f() const { return x; }
};
int main()
{
A a(15);
cout << a.f();
return 0;
}
它创建了一个名为a的对象,并将其x成员的值设置为15。但是,当我尝试调用该函数以返回该值时,我得到了一个错误。为什么会发生这种情况?
我刚开始学习编程,我们才刚开始学习“课程”。我要给大家看一个我在网上找到的代码示例。我的问题是-- "add“和" res "构造函数是什么?构造函数怎么可能返回值?"X res”和X add"不是int类型的方法,它仍然返回一个值(res也没有变量),所以我真的很困惑。“我在堆栈溢出的几个帖子中看到,构造函数不能返回值,但是“then和X add"是什么呢?
#include <iostream>
using namespace std;
class X {
int a;
int b;
public:
X
我在试着理解在析构函数中允许做什么。
说:“对于具有非平凡析构函数的对象,在析构函数完成执行后引用对象的任何非静态成员或基类会导致未定义的行为”。
以这种方式描述销毁序列:“对于用户定义或隐式定义的析构函数,在执行析构函数的主体后,编译器调用类的所有非静态非变体成员的析构函数”。
这是否意味着,在下面的代码中,从其成员的析构函数调用方法是UB?或者说“参照”标准指的是一些特殊的东西?
struct Foo {
Foo(Callback cb) : cb_(cb) {}
~Foo() {
// body of Bar destructor finished at this mo
这个简单程序的输出是This is base。
public class mainApp{
private void func(){
System.out.println("This is base");
}
public static void main(String[] args){
mainApp newObj = new derived();
newObj.func();
}
}
class derived extends mainApp{
public void func(){
这是一个解释问题的示例代码。
为什么"display()“不调用成员函数,而是调用非成员函数?
#include<iostream>
using namespace std;
class foo
{
private:
int num;
public:
// Constructor
foo() : num(0){}
// Member function
void display()
如果我有一个类:
class A {
public:
A();
~A();
int something();
}
class B {
public:
B() {a = new A();};
~B();
private:
A *a;
}
main() {
B x;
}
在main中使用对象"a“的唯一方法是使用它的getter吗?
class B {
public:
A getA() {return *a;};
private:
A *a;
}
然后,如果我想设置私有变量对象,我需要一个setter,并在main
在标准中,公开究竟意味着什么?这是否意味着标记为公开的私有/受保护成员按标准只需要存在,还是它们只是实现的“建议”,根本不需要?
示例包括:
std::error_code::val_
std::wstring_convert::byte_err_string
std::array::elems
std::move_iterator::current
std::reverse_iterator::current
std::ostream_iterator::delim
// And a lot of others