我的问题很简单(这并不意味着答案会很简单..:D)
为什么C++中的数组包含大小作为类型的一部分而Java不包含?
我知道Java数组引用变量只是指向堆上数组的指针,但C++指向数组的指针也是如此,但我需要提供一个大小.让我们先分析一下C++:
// in C++ :
// an array on the stack:
int array[*constexpr*];
// a bidimensional array on the stack:
int m_array[*constexpr1*][*constexpr2*];
// a multidimensional array on the stack:
int mm_array[*constexpr1*][*constexpr2*][*constexpr3*];
// a dynamic "array" on the heap:
int *array = new int[n];
// a dynamic bidimensional "array" on the heap:
int (*m_array)[*constexpr*] = new int[n][*constexpr*];
// a dynamic multidimensional "array" on the heap:
int (*mm_array)[*constexpr*][*constexpr*] = new int [n][*constexpr1*][*constexpr2*];
Run Code Online (Sandbox Code Playgroud)
n不必是编译时常量表达式,所有元素都是默认初始化的.动态分配的"数组"不是类型数组,但新表达式产生指向第一个元素的指针.
因此,当我创建一个动态数组时,除了第一个维度之外的所有维度都必须是常量表达式(否则我无法声明指针来保存它们的元素).这样对吗??
现在到Java.I只能在堆上分配数组,因为这是Java的工作方式:
// a dynamic array on …Run Code Online (Sandbox Code Playgroud) 我正在研究用C++转换构造函数和转换运算符.到目前为止我学到的是,任何只带一个参数(以及任意数量的可选默认参数)的非显式构造函数表示对THAT类类型的隐式类类型转换,例如,如果类定义了一个构造函数,类型intI的一个参数可以int在需要该类类型的对象的任何地方使用:
(假设class_type有一个重载的+ =运算符)
class_type a;
a+=5;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,5被隐式转换(通过转换构造函数),class_type并调用重载的运算符.
现在,(至少对我来说)棘手的部分:我知道我可以将转换运算符定义为成员函数:
operator int() {....};
Run Code Online (Sandbox Code Playgroud)
将对象转换为class_type基本int类型,我可以使用如下转换:
class_type a;
a+5;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我已经读过通过其转换运算符将对象转换为int,然后调用内置和运算符.但是如果我定义了一个重载的+运算符来将两个class_type对象作为其参数呢?就像是
class_type operator+(const class_type&,const class_type &c);
编译器如何知道通过函数匹配调用哪一个?int仅当定义了内置运算符时,转换是否仅隐式发生?
谢谢!
编辑:
实际上,我已经尝试编写一些代码来有效地尝试它,结果发现我的编译器(g ++)不会发出任何模糊的调用错误!
这是类头(以及非memeber操作符+函数声明):
#include <iostream>
class wrapper {
friend std::ostream &operator<<(std::ostream&,const wrapper&);
public:
wrapper()=default;
wrapper(int);
int get();
operator int() const;
wrapper operator+(int);
private:
int a=10;
};
std::ostream &operator<<(std::ostream&,const wrapper&);
Run Code Online (Sandbox Code Playgroud)
这是主要代码:
#include "wrapper.h"
int main()
{
using namespace std;
wrapper w1;
wrapper w2(5);
cout<<w1<<" "<<w2<<endl; …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个简单的 DirectX11 引擎,但我一直收到这个奇怪的错误,但我找不到问题所在:我定义了一个 Terrain 类和一个 Mesh 类,并在 Terrain 类中 #include Mesh 类:
地形类定义:
// Terrain.h
#pragma once
#include "Noise.h"
#include "Mesh.h"
class Terrain
{
public:
Terrain(float width, float depth, int numVerticesW, int numVerticesD);
~Terrain();
float GetHeight(float x, float z);
void Draw();
private:
Mesh mMesh; // I get the error on this line
Noise mNoiseGenerator;
std::vector<float> mHeights;
void CreateTerrain(float width, float depth, int numVerticesW, int numVerticesD);
float ComputeHeight(float x, float z, float startFrequency, float startAmplitude, float persistence, int octaves);
};
Run Code Online (Sandbox Code Playgroud)
和 …
我正在学习C++语法,并且我已经到了我正在研究数组的地步.我想问你一个问题,但首先让我回顾一下,所以我知道我得到了这些东西.我知道您可以使用以下语法将变量定义为数组:
<base type> name [<size(constexpr)>]
Run Code Online (Sandbox Code Playgroud)
(大小是数组类型的一部分).这将给我一个基本类型的大小元素数组 如果我想要一个指向基类型的指针数组,我可以在基本类型说明符之后添加一个*,就像在普通指针声明中一样.
<base type> *name [<size(constexpr)>]
Run Code Online (Sandbox Code Playgroud)
我无法定义引用数组,因为aray应该只包含对象(而引用只是别名).
现在,如果我想声明一个引用或指向数组的指针,我可以使用以下语法:
<base type> (&name) [<size(constexpr)>]
Run Code Online (Sandbox Code Playgroud)
要么
<base type> (*name) [<size(constexpr)>]
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很清楚.我也知道,我可以将数组作为参数传递给函数,但该调用将始终被解释为我传递指向数组元素类型的指针.声明如下的函数:
void f(int array[10])
Run Code Online (Sandbox Code Playgroud)
是相同的:
void f(int array[])
void f(int *p)
Run Code Online (Sandbox Code Playgroud)
每当我调用这个函数时,我总是传递一个int*(传递值).
问题:如果我想在不使用参数名称的情况下编写函数原型(纯声明),该怎么办?我知道在这种情况下我通常可以省略参数名称(我可以写void f(int*)但是其他两个声明呢?)更重要的是,如果参数是引用或指向数组的指针怎么办?
void f(int (&array)[])
Run Code Online (Sandbox Code Playgroud)
要么
void f(int (*array)[])
Run Code Online (Sandbox Code Playgroud)
谢谢!
我知道在 Java 泛型中,当使用具有多个边界的类型参数时,编译器会将类型信息擦除到“最左边的边界”(即列表中的第一个类/枚举或接口)。那么为什么下面的代码编译没有问题呢?
public class Generic<T extends Object & Appendable & AutoCloseable> {
T t;
T method() throws Exception {
t.close();
char c='\u0000';
t.append(c);
return t;
}
public <T> T method2(T t) {
return t;
}
}
Run Code Online (Sandbox Code Playgroud)
类型参数 T 不应该被视为 Object 吗??(因此不允许我调用 close() 或 append())??
我正在尝试为一个C风格的字符串数组(一个指向char的指针数组)获取一个开头和一个结束指针.为什么我不能调用开始和结束函数来获取它们?
#include <iostream>
#include <iterator>
int main(int argc,char *argv[])
{
char **first=std::begin(argv),**last=std::end(argv);
}
Run Code Online (Sandbox Code Playgroud)
编译器说我的调用没有匹配功能 begin(char**&)
我是C++的新手,我正在学习lambdas,functor和callables,我知道有一个包装类,即std::function允许存储和调用不同类型的callables(只要具有相同的调用签名,或功能类型).
现在,我明白你可以使用函数类型参数来实现函数指针参数,如下所示:
void fun(int,int*(int,int&));
Run Code Online (Sandbox Code Playgroud)
它只不过是一个函数,它接受一个int函数指针和一个函数指针int *f(int,int&),即使该语言允许我将函数作为参数传递(带或不带符号).事实上,函数参数列表也可能写成:
void fun(int,int*(*)(int,int&));
Run Code Online (Sandbox Code Playgroud)
现在,回到std::function类型
我知道我可以std::function使用函数类型进行实例化,并允许将任何类型的可调用函数传递给包装器.但是,函数类型不是我可以在任何实例化中用作模板类型参数的类型,例如:
std::vector<int(int)> f_vec;
Run Code Online (Sandbox Code Playgroud)
相反,我应该制作一个函数指针的向量
std::vector<int(*)(int)> f_vec;
Run Code Online (Sandbox Code Playgroud)
这将允许我插入指向函数的指针,但不能插入函子或lambda.
所以,我的问题是,如何使用类型参数实例化模板,如函数类型?在库std::function类型的引擎盖下发生了什么.我的意思是一个函数类型在我看来是一个我不能在模板中使用的类型?请求你能让事情变得更加清晰,因为我刚开始学习这些话题.谢谢
我有以下代码片段:
int x=2,y=3;
if ( (y == x++) | (x < ++y) )
// rest of code
Run Code Online (Sandbox Code Playgroud)
我知道在C++中,你被教导不要依赖子表达式的评估顺序,因为它根本不保证是任何顺序.所以这段代码会出错,并且条件中表达式产生的布尔值不能保证为真(例如,y可以在第一次相等性测试中计算之前递增).由于我在Java认证书中阅读了这段代码,我认为Java不是这种情况.我的意思是,我保证Java中的评估顺序总是从左到右吗?所以上面的表达式总是应该是真的.
我试图了解模板专业化是如何工作的。我有以下功能模板:
template <typename T>
void function(const T &t1, const T &t2)
{
std::cout << "in function template: " << t1 << ", " << t2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想专门化这个函数模板,以防它被一个指向 const 的指针调用:
// template specialization
template <>
void function(const char *&t1, const char *&t2)
{
std::cout << "in compare template specialization: " << t1 << ", " << t2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器抱怨它找不到专门化的函数模板:
In file included from main.cpp:1:0:
template.h:23:5: error: template-id 'compare<>' for 'int compare(const char*&, const char*&)' does not match …Run Code Online (Sandbox Code Playgroud) 我正在学习C++,偶尔会碰到像"伴侣类型"或某些"机器相关的整体类型"这样的东西,它们会在不同的场合使用,例如:
vector<int>::size_type
Run Code Online (Sandbox Code Playgroud)
要么
std::size_t
Run Code Online (Sandbox Code Playgroud)
或者最近我正在阅读关于IO类型和对象的内容,我读到了一个"由IO类定义"的类型,例如:
istream::iostate
Run Code Online (Sandbox Code Playgroud)
这显然是某种用于指示流对象状态的位模式所使用的整数类型.
我知道类可以包含(除了成员函数和数据成员)类型成员,以类型别名声明(typedef和using别名声明)的形式,但它对我来说没有任何意义,必须有一些我没有读过的东西关于.你能解释一下吗?
非常感谢!
c++ ×9
arrays ×3
java ×3
c++11 ×2
pointers ×2
templates ×2
expression ×1
generics ×1
include ×1
lambda ×1
reference ×1
std-function ×1
type-bounds ×1
type-members ×1
types ×1