How to use instance of one class in a method of another class?

I defined two classes class A and class B . They are completely independent. Now I create c as instance of class B and d as instance of class A . Now I want to define the body of functionOfA that will perform operations on c : class A { public: functionOfA(); } class B { ... } A::functionOFA() { c.functionOfB(); } main() { A d; B c; d.functionOfA(); } but the comp

如何在另一个类的方法中使用一个类的实例?

我定义了两个类A和B。 他们是完全独立的。 现在我创建c作为class B实例,并将d作为class A实例。 现在我想定义将在c :上执行操作的functionOfA的主体。 class A { public: functionOfA(); } class B { ... } A::functionOFA() { c.functionOfB(); } main() { A d; B c; d.functionOfA(); } 但是编译器给了我下面的错误: c没有在这个范围内声明 A::functionOfA()定义需要知道什么是c实例,你

How *val and &val differ when passed by reference in C++

void funct( int *val1, int &val2) { *val1= 20; val2= 22; } int main() { int v1=10, v2=12; funct(&v1, v2); cout<<"v1: "<<v1<<endl<<"v2: "<<v2<<endl; return 1; } These both, v1 and v2 are called by reference by funct(). But I don't understand, what is difference between both ways apart from syntactical difference. Few questi

当通过C ++中的引用传递时,* val和&val的区别如何?

void funct( int *val1, int &val2) { *val1= 20; val2= 22; } int main() { int v1=10, v2=12; funct(&v1, v2); cout<<"v1: "<<v1<<endl<<"v2: "<<v2<<endl; return 1; } 这两个,v1和v2通过funct()引用来调用。 但我不明白,除了语法上的区别之外,两种方式之间有什么区别。 我想问的几个问题是, 两者都完全一样吗? 在编码实践方面都同样好?

(C++) Passing a pointer into a template

I have two pointers. p1. p2. p1 and p2 both point to different classes. The classes have some similar method names, and I'd like to call a template function twice to avoid repeated code. Here is my function: template <class T> void Function(vector<T> & list, T* item, const string & itemName) see that middle paramater, "item".. is that how my signature

(C ++)将指针传递给模板

我有两个指针。 P1。 P2。 p1和p2都指向不同的类。 这些类有一些类似的方法名称, 我想两次调用模板函数以避免重复的代码。 这是我的功能: template <class T> void Function(vector<T> & list, T* item, const string & itemName) 看到中间参数,“项目”..是我的签名应该看起来如果我想要项目改变? ..或者我应该通过它 T *&item ..或者我应该通过它 T **项目 编译器会让很多东西滑

Pointer size and Pass

I have two questions: Am I right that on 4-bit systems, a pointer is 4 bytes? Are "pass by reference" and "pass by pointer the same thing, just different wording? Am I right that on 4-bit system a pointer size is 4 If your system has 1-bit bytes, then for sure it is. (But C doesn't support platforms where bytes are shorter than 8 bit anyway.) Is "pass by referen

指针大小和通行证

我有两个问题: 我是对的,在4位系统上,指针是4个字节? 是“通过引用传递”和“通过指针传递相同的东西,只是不同的措辞? 我是对的,在4位系统上,指针大小是4 如果你的系统有1位字节,那肯定是。 (但C不支持字节长度小于8位的平台。) 是“通过引用”和“通过指针”相同的东西,但措辞不同? 否。传递指针是一种用于模拟传递引用的C方法。 这个概念是不同的。 指针的大小不一定与CPU的本地字大小相关; 例如,原始

Why and when to pass class types in C++ by pointer?

Consider the following code: class Abstract{ public: virtual void printStuff()=0; }; class Derived: public Abstract{ public: void printStuff(){ printf("Stuffn"); } }; Now, let's say I want to create a function that uses the printStuff method from Abstract class. Before I learned that only one way is possible in C++, I thought that there would be two ways: the less obvi

为什么以及何时通过指针在C ++中传递类类型?

考虑下面的代码: class Abstract{ public: virtual void printStuff()=0; }; class Derived: public Abstract{ public: void printStuff(){ printf("Stuffn"); } }; 现在,我们假设我想创建一个使用Abstract类中的printStuff方法的函数。 在我发现在C ++中只有一种可能的方式之前,我认为会有两种方式:指针不那么明显,更明显,类似于你对int和chars等的期望: void ptr_function(Abstract* abs){ //n

Passing by reference options in C++

I want to pass an object of Class A (call it a) by reference (in the broad sense, ie either by A& or by A*) to the constructor of another Class B. I do not want 'a' to be modified inside B ('a' is read only and it is big in size which is why I want to pass it by reference). I know of two options: 1) Pass 'a' as const A & a_ 2) Pass 'a' as const A * a_

在C ++中通过引用选项传递

我想通过引用(广义上,即通过A和或A *)传递类A的对象(称为a)给另一个类B的构造函数。我不想在内部修改'a' B('a'是只读的,它的大小很大,这就是为什么我要通过引用传递它)。 我知道两个选择: 1)通过'a' const A & a_ 2)通过'a' const A * a_ 选项1的缺点是我可能错误地传递了一个r值。 选项2的缺点是我可能会错误地传递一个空指针。 我的问题是:1)我对上述缺点是否正

C++ function parameters: use a reference or a pointer (and then dereference)?

I was given some code in which some of the parameters are pointers, and then the pointers are dereferenced to provide values. I was concerned that the pointer dereferencing would cost cycles, but after looking at a previous StackOverflow article: How expensive is it to dereference a pointer?, perhaps it doesn't matter. Here are some examples: bool MyFunc1(int * val1, int * val2) { *v

C ++函数参数:使用引用还是指针(然后解引用)?

给了我一些代码,其中一些参数是指针,然后指针被解引用来提供值。 我担心指针取消引用会花费几个周期,但是在查看以前的一篇StackOverflow文章之后:取消引用指针的代价有多高?可能没关系。 这里有些例子: bool MyFunc1(int * val1, int * val2) { *val1 = 5; *val2 = 10; return true; } bool MyFunc2(int &val1, int &val2) { val1 = 5; val2 = 10; return true; } 我个人比较喜欢传递引用

difference between a pointer and reference parameter?

Are these the same: int foo(bar* p) { return p->someInt(); } and int foo(bar& r) { return r.someInt(); } Ignore the null pointer potential. Are these two functions functionally identical no matter if someInt() is virtual or if they are passed a bar or a subclass of bar ? Does this slice anything: bar& ref = *ptr_to_bar; C++ references are intentionally not specified in the

指针和参考参数之间的区别?

这些都是一样的: int foo(bar* p) { return p->someInt(); } 和 int foo(bar& r) { return r.someInt(); } 忽略空指针的潜力。 是这两个函数功能相同不管someInt()是虚拟的,或者如果它们传递一bar或子类bar ? 这是否切片: bar& ref = *ptr_to_bar; 故意在标准中没有指定使用指针来实现C ++引用。 引用更像是一个变量的“同义词”,而不是指向它的指针。 这种语义为编译器打开了一些可能的优化,因为

Is always passing by reference a bad practice?

This question already has an answer here: Are there benefits of passing by pointer over passing by reference in C++? 6 answers One common style is to pass input arguments by const reference, and output arguments by pointer. In this style, we don't pass output arguments by non-const reference to more clearly delineate input from output arguments.

总是通过引用传递一个不好的做法?

这个问题在这里已经有了答案: 在C ++中通过引用传递指针会带来什么好处? 6个答案 一种常见的风格是通过const引用传递输入参数,并通过指针输出参数。 在这种风格中,我们不通过非const引用传递输出参数,以更清楚地描述输出参数的输入。

Double pointer vs pass by reference pointer

This question already has an answer here: When to use references vs. pointers 17 answers Are there benefits of passing by pointer over passing by reference in C++? 6 answers "Why using reference to pointer instead of pointer to pointer"? You will get the same answer as if asking "why using pointer instead of reference" for any other kind of variable... Basically: r

双指针vs通过引用指针传递

这个问题在这里已经有了答案: 何时使用引用与指针17的答案 在C ++中通过引用传递指针会带来什么好处? 6个答案 “为什么使用引用而不是指针指针”? 你会得到相同的答案,就像询问“为什么使用指针而不是参考”一样,用于任何其他类型的变量...... 基本上: 对指针或任何其他变量的引用很聪明,因为后面应该总是有一个对象 指针(指针或任何其他变量)是智能的,因为它们可能是NULL (可选) 引用(对指针或任何其