我试图围绕C与Objective-C中的使用和语法的一些差异.特别是,我想知道C运算符和Objective-C中的点运算符和箭头运算符的使用方式有何不同(以及原因).这是一个简单的例子.
C代码:
// declare a pointer to a Fraction
struct Fraction *frac;
...
// reference an 'instance' variable
int n = (*frac).numerator; // these two expressions
int n = frac->numerator; // are equivalent
Run Code Online (Sandbox Code Playgroud)
Objective-C代码:
// declare a pointer to a Fraction
Fraction *frac = [[Fraction alloc] init];
...
// reference an instance variable
int n = frac.numerator; // why isn't this (*frac).numerator or frac->numerator??
Run Code Online (Sandbox Code Playgroud)
那么,看看frac
两个程序中的相同情况(即它是指向Fraction对象或结构的指针),为什么它们在访问属性时使用不同的语法?特别是,在C中,numerator
可以访问属性frac->numerator
,但是使用Objective-C,可以使用点运算符访问它frac.numerator
.既然frac
是两个程序中的指针,为什么这些表达式不同?任何人都可以帮我澄清一下吗?
MATLAB数组支持矩阵运算和元素运算.例如,M*N
和M.*N
.这是区分两种不同操作的非常直观的方式.如果我想在C++中实现类似的操作,我该怎么做?
我也可以创建一个新的运营商.*
吗?如果是的话,有人可以给我一些指导吗?
我发现使用C++ STL方法进行简单的集合操作非常笨重.例如,要找到两组之间的差异:
std::set<int> newUserIds;
set_difference(currentUserIds.begin(), currentUserIds.end(), mPreviousUserIds.begin(), mPreviousUserIds.end(), std::inserter(newUserIds, newUserIds.end()));
std::set<int> missingUserIds;
set_difference(mPreviousUserIds.begin(), mPreviousUserIds.end(), currentUserIds.begin(), currentUserIds.end(), std::inserter(missingUserIds, missingUserIds.end()));
mPreviousUserIds = currentUserIds;
Run Code Online (Sandbox Code Playgroud)
boost是否提供了一组替代类,可以将上面的示例缩小为:
set_type<int> newUserIds = currentUserIds.difference(mPreviousUserIds);
set_type<int> missingUserIds = mPreviousUserIds.difference(currentUserIds);
Run Code Online (Sandbox Code Playgroud)
(类似于Qt中的QSet,它operator-
以这种方式覆盖.)
我很难理解stroustrup对于必须遇到什么困难的解释,如果运算符重载'.' 曾经被允许.
请参阅Bjarne Stroustrup的这句话:
运营商.(dot)原则上可以使用与 - >相同的技术重载.但是,这样做会导致对操作是否意味着对象重载的问题.或者提到的对象.例如:
class Y {
public:
void f();
// ...
};
class X { // assume that you can overload .
Y* p;
Y& operator.() { return *p; }
void f();
// ...
};
void g(X& x)
{
x.f(); // X::f or Y::f or error?
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,为什么在执行时会有任何混淆x.f()
?
Y& operator.() { return *p; }
Run Code Online (Sandbox Code Playgroud)
这是我的想法:
Y& operator.()( return *p; }
不应该被称为obivous和直觉吗?*p
指向Y类型的对象,因此Y::f()
应该最终调用(不是X::f()
)我在stroustup的解释中缺少什么?为什么不直截了当?
我正在编写一个简单的哈希映射类:
template <class K, class V> class HashMap;
Run Code Online (Sandbox Code Playgroud)
实现是非常正统的:我有一个堆数组,当它变大时,它的大小翻倍.该数组包含键/值对的小向量.
Vector<Pair<K, V> > *buckets;
Run Code Online (Sandbox Code Playgroud)
我想以这样的方式重载下标运算符,这样的代码将起作用:
HashMap<int, int> m;
m[0] = 10; m[0] = 20;
m[2] = m[1] = m[0];
Run Code Online (Sandbox Code Playgroud)
特别是,
m[k] = v
其中m
不包含k
,我想添加一个新条目.m[k] = v
其中m
确实含有k
,我想旧值被替换.v
.据推测,代码看起来像
V& operator[](K &key)
{
if (contains(key))
{
// the easy case
// return a reference to the associated value
}
else
{
Vector<Pair<K, V> > *buck = buckets + …
Run Code Online (Sandbox Code Playgroud) 我不得不花费一些时间来查找并修复我在以下代码中设法隔离的错误:
#include <iostream>
struct A
{
std::string S;
A(const std::string s) { S = s; }
};
void f1(A a) { std::cout << "f1:a.S = " << a.S << "\n"; }
void f1(const std::string s) { std::cout << "f1:s = " << s << "\n"; }
void f2(A a) { std::cout << "f2:a.S = " << a.S << "\n"; }
int main()
{
f1(A("test"));
f1(std::string("test"));
f2(A("test"));
f2(std::string("test"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个错误是由f1
函数创建的被忽略的(由我和编译器(?))模糊引起的:f2
清楚地显示了这两者f1(A)
并且f1(std::string)
适用于A …
我正在阅读Stanley B. Lippman的C++ Primer一书,在变量和基本类型部分,我看到了范围运算符::
.
我已经阅读了一些关于运算符重载的内容,我认为它在特殊情况下非常有用,但是当我在互联网上搜索时,我发现我根本无法使::
运算符超载.
在这篇文章中,我发现.
操作员可能过载.但是,这可能导致关于操作是针对对象重载.
还是针对所引用的对象的问题.
.
因此,我认为可能有一种方法可以超载::
.
但如果它不能,任何人都可以解释我为什么?
我对::运算符的想法的一个例子:
#include <iostream>
/*
*For example:
*I wanna increase 1 unit every time
*I call the global variable r doing ::r
*insede of the main function
*/
int r = 42;
int main()
{
int r = 0;
std::cout << ::r << " " << r << std::endl; //It would print 43 0 after the operator overload
return …
Run Code Online (Sandbox Code Playgroud) #define EXTERNAL_API_VERSION 1.12.1
std::string version = boost::lexical_cast<std::string>(EXTERNAL_API_VERSION);
Run Code Online (Sandbox Code Playgroud)
此代码生成编译错误:
error C2143: syntax error : missing ')' before 'constant'
error C2059: syntax error : ')'
Run Code Online (Sandbox Code Playgroud)
是否有任何简单的替代方法可以将这种格式(多于一个点)的数字转换为字符串?
我希望能够写出像a = bc这样的陈述; 其中b和c是std :: vector,a是它们的标量点积(double).为此,我应该将点积运算与'.'联系起来.符号.这可能吗?