我有 2 个包含方法的静态类
public static class SMS {
}
和
public static class Contacts {
}
我想创建一个静态电话类,以便我可以这样引用它:
Phone.SMS.SendSMS(phoneNumber, text);
请告诉我该怎么做。
这不是我第一次提出关于折线问题的问题。这次我想在我的代码中讨论“C++ 中的复制和交换成语”。如果您有兴趣,可以在此处阅读问题说明。
因此,在我的代码中,我实现了 Copy-and-Swap 习惯用法:
void swap(Loman& a) {
std::swap(size, a.size);
std::swap(data, a.data);
}
Loman& operator=(const Loman& a)
{
Loman tmp(a);
swap(tmp);
return *this;
}
但在我看来,这段代码中有一个逻辑错字,实际上应该这样写:
void swap(Loman& first, Loman& second) {
std::swap(first.size, second.size);
std::swap(first.data, second.data);
}
Loman& operator=(const Loman& a)
{
Loman tmp(a);
swap(*this, tmp);
return *this;
}
在我看来, + 运算符中还有一个不必要的操作this-> size = size
。如果我删除它,什么都不会改变。
这是整个代码:
#include <iostream>
using namespace std;
class Loman
{
private:
int i=0;
int size;
int* data;
public:
Loman(int asize) {
size = 2*asize;
data = new int[size];
}
~Loman(){
delete[] data;
cout<<"Object destroyed\n";
}
Loman(const Loman &ob)
{
size = ob.size;
data = new int [size];
for(int i=0; i<ob.size; i++)
data[i] = ob.data[i];
}
void print(){
for(i=0; i<size; i++){
cout<<*(data+i)<<" ";
}
cout<<endl;
}
void scan(){
cout<<"Input "<<size/2<<" coordinates\n";
for(int i=0; i<size; i++){
cin>>*(data+i);
}
}
Loman operator+(Loman& ob){
this->size = size;
Loman tmp((size + ob.size)/2);
for(i=0; i<size; i++)
{
tmp.data[i]=*(data + i);//this->data[i]=data[i];
}
for(i=size; i<size + ob.size; i++) tmp.data[i] = ob.data[i-size];
return tmp;
}
void swap(Loman& a) {
std::swap(size, a.size);
std::swap(data, a.data);
}
Loman& operator=(const Loman& a)
{
Loman tmp(a);
swap(tmp);
return *this;
}
};
int main(){
Loman first(3);
Loman second(2);
first.scan();
second.scan();
Loman third(5);
third=first+second;
third.print();
return 0;
}
问题:我关于“+”运算符和交换函数的假设是否正确,如果不是,为什么不呢?
我错过了任何其他错误吗?
是否需要在swap函数中写using std::swap;
#include <iostream>
class Character
{
private:
short x = -1;
short y = -1;
};
class Luchnik : public Character
{
public:
Luchnik(short x, short y) {
this->x = x;
this->y = y;
}
private:
short x = 0;
short y = 0;
};
int main() {
Luchnik *l = new Luchnik(1, 2);
Character a();
&a = l;
delete l;
return 0;
}
visual stuido 写入表达式必须有效才能更改左侧值。
这是屏幕截图
更新
或者您可以在 Luchnik 构造函数中获取一个向量指针并将 vector[x][y] 对象更改为 Luchnil
我是这样写的,但是不行,看不懂指针和类的东西
Luchnik(short x, short y, std::vector<std::vector<Character>> map) {
this->x = x;
this->y = y;
Character* pm = &(map[x][y]);
pm = *this;
}
int main() {
std::vector<std::vector<Character>> board = new std::vector<std::vector<Character>>(3, std::vector<Character>(4));
Luchnik l(1, 2, *board);
return 0;
}
书中的代码略有更新。我不知道为什么它会出错,最令人惊讶的是,它会在不是我编写的代码中出错!
#include <iostream>
using namespace std;
char* mycpy(char* word_1, const char* word_2) {
while (*word_1++ = *word_2++);
return word_1;
}
class TwoDShape {
double width, height;
char name[20];
public:
TwoDShape() {
width = height = 0, 0;
mycpy(name, "no");
}
TwoDShape(double w, double h, const char* n) {
width = w;
height = h;
mycpy(name, n);
}
TwoDShape(double x, const char* n) {
width = height = x;
mycpy(name, n);
}
void showDim() {
cout << "Ширина и высота составляют " << width << "и " << height << "\n";
}
double getWidth() { return width; }
double getHeight() { return height; }
void setWigth(double w) { width = w; }
void setHeight(double h) { height = h; }
void setName(const char* n) { mycpy(name, n); }
char* getName() { return name; }
virtual double area() = 0;
};
class Triangle : public TwoDShape {
char style[20];
public:
Triangle() {
mycpy(style, "no");
}
Triangle(const char* str, double w, double h) : TwoDShape(w, h, "треугольник") {
mycpy(style, str);
}
Triangle(double x) : TwoDShape(x, "треугольник") {
mycpy(style, "равнобедренный");
}
void showStyle() {
cout << "Треугольник " << style << "\n";
}
double area() {
return getWidth() * getHeight() / 2;
}
};
class Rectangle : public TwoDShape {
public:
Rectangle(double w, double h) : TwoDShape(w, h, "Прямоугольник"){ }
Rectangle(double x) : TwoDShape(x, "Прямоугольник") { }
bool isSquare() {
if (getWidth() == getHeight()) return true;
return false;
}
double area() {
return getWidth() * getHeight();
}
};
class Circle : public TwoDShape {
double R;
public:
Circle(double r) : TwoDShape() {
R = r;
setName("Кргу");
}
double area() {
return (3.14 * (R * R));
}
};
int main() {
setlocale(LC_ALL, "ru");
TwoDShape* p_shapes[5];
p_shapes[0] = &Triangle("Прямоугольный", 8.0, 12.0); // <------------ Ошибка
p_shapes[1] = &Rectangle(10);// <------------ Ошибка
p_shapes[2] = &Rectangle(10, 4);// <------------ Ошибка
p_shapes[3] = &Triangle(7.0);// <------------ Ошибка
p_shapes[4] = &Circle(33.0);// <------------ Ошибка
for (int i = 0; i < 4; i++) {
cout << "Объект представляет собой " << p_shapes[i]->getName() << "\n";
cout << "Площадь равна " << p_shapes[i]->area() << "\n";
cout << "\n";
}
}
假设我们有一些课程:
class Base
{
public:
INLINE static int get_something()
{
return 777;
}
//....
};
并且还声明了以下模板类 A:
template <class TBase>
class A
{
public:
template <typename TObject>
INLINE static TObject get_something()
{
return (TObject)(TBase::get_something() + 1);
}
//....
};
现在我们可以在这样的代码中使用它:
double a = A<Base>::get_something<double>();
printf("A: %lf\n", a);
太好了,一切正常!但是,现在让我们添加一个模板类 B,它可以被认为是 A 的某种包装器:
template <class TBase>
class B
{
public:
template <typename TObject>
INLINE static TObject get_something()
{
return (TObject)(A<TBase>::get_something<TObject>() + 1); //Тут компилятор ругается!
}
//....
};
我们立即从编译器中得到:
main.cpp: In static member function 'static TObject B<TBase>::get_something()':
main.cpp:38:57: error: expected primary-expression before '>' token
38 | return (TObject)(A<TBase>::get_something<TObject>() + 1);
| ^
main.cpp:38:59: error: expected primary-expression before ')' token
38 | return (TObject)(A<TBase>::get_something<TObject>() + 1);
同时,如果在表达式中我们A<TBase>::get_something<TObject>()
将模板参数替换TBase
为显式类型Base
,那么一切都会再次起作用:
template <class TBase>
class B
{
public:
template <typename TObject>
INLINE static TObject get_something()
{
return (TObject)(A<Base>::get_something<TObject>() + 1); //А тут всё хорошо...
}
//....
};
//...и мы можем это использовать в коде
float b = B<Base>::get_something<float>();
printf("B: %f\n", b);
问题是,编译器不喜欢什么以及如何使其正确取消引用类模板参数A,其函数是从模板类B的模板函数调用的?类似的东西。