简体   繁体   English

用C ++构造对象

[英]Constructing objects in c++

So, I'm coming from Java, learning c++. 所以,我来自Java,正在学习c ++。 I want to implement a program that can be called like this, provided by a test file (boots): 我想实现一个可以由测试文件(引导)提供的,可以这样调用的程序:

auto subject = anagram::anagram("diaper");
auto matches = subject.matches({"hello", "world", "zombies","pants"});
vector<string> expected;
BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), matches.begin(), matches.end());

Do I get it right, that anagram::anagram(...) is a constructor, constructing an Object that provides the method matches(...) ? 我正确吗,那anagram::anagram(...)是一个构造函数,构造了一个提供方法matches(...)的Object? Because in my Implementation (cf. below) I get an error saying something (not in english) like "constructor can not be called directly". 因为在我的实现中(请参见下文),我遇到一个错误,说“不能直接调用构造函数”之类的东西(不是英语)。 I think there is something I do not understand about constructors in c++. 我认为对于c ++中的构造函数我有些不了解。

// this is part of my implementation of anagram.h
class anagram{

public:
    anagram(const string a);
    vector<string> matches(vector<string> &list);

private:
    string a;
    bool isAnagram(string s);    
};

The only other thing I know is that anagram::anagram(...) could be a static method, with a return value that is similat to this , but that wouldn't make sense to me. 我唯一知道的另一件事是anagram::anagram(...)可以是静态方法,其返回值与this相似,但是对我而言这没有意义。 So it would be nice if someone could explain this :) 因此,如果有人可以解释这个问题,那就太好了:)

Because you come from java you are used to see something similiar like this: 因为您来自Java,所以习惯于看到类似以下内容:

 someobject T = new someObject(argument);

in C++ you construct a class in a different way: 在C ++中,您以不同的方式构造类:

 someObject T(argument);

and tada you created an Object with name T in c++. 和tada,您在c ++中创建了一个名称为T的对象。 an other way is to create a pointer to an object: 另一种方法是创建指向对象的指针:

someObject *T = new someObject(argument);

which is more similar to java but now you have to delete that pointer manually. 它更类似于java,但是现在您必须手动删除该指针。 It is always better to create objects on the stack rather the heap. 最好在堆栈而不是堆上创建对象。 it avoids memory leaks. 它避免了内存泄漏。


To answer you question: You got it wrong. 回答您的问题:您错了。 You call Constructors like i showed you earlier. 您称呼我之前向您展示的构造函数。 you are right about how to call a static memberfunction but this does not apply for the Ctor because it can never be static nor virtual. 您对如何调用静态成员函数是正确的,但这不适用于Ctor,因为它永远不能是静态或虚拟的。

If the call anagram::anagram must work anagram is a namespace inside it a function called anagram is defined to return an object onto which you can call a method matches : 如果调用anagram::anagram必须正常工作anagram是其中的一个命名空间,则定义了一个称为anagram的函数,以返回一个对象,您可以在该对象上调用matches方法:

namespace anagram {
  SomeType anagram(string n) { return SomeType(n); }
};

That also could be a class name anagram inside a namespace anagram . 这也可能是一个类名anagram在命名空间里anagram

That couldn't be a static method as no method except ctors can use the name of the class as identifier. 那不可能是静态方法,因为除了ctor可以使用类的名称作为标识符之外,没有方法可以使用。

One way it can work with syntax you have shown is if class anagram is placed in similary named namespace: 它可以与您显示的语法一起使用的一种方法是,将类anagram放置在类似命名空间中:

#include <string>
namespace anagram 
{
class anagram
{
  public:
    anagram(std::string s) {}
};
}

int main() 
{
    auto subject = anagram::anagram("diaper");
}

http://ideone.com/1OVhIz http://ideone.com/1OVhIz

The syntax would actually look a similar way as in Java (except for the new , of course) : 该语法实际上看起来与Java中的语法类似(当然,除了new之外):

auto subject = Anagram("diaper");

But there's an easier way: 但是有一种更简单的方法:

Anagram subject("diaper");
Anagram subject{"diaper"}; // C++11 and newer

Also do note that if Anagram has a default constructor, it will be constructed when you define it - It can't be null or something like in java, unless you are using * pointers. 还要注意,如果Anagram具有默认构造函数,则在定义它时将对其进行构造-不能为null或类似Java的构造函数,除非您使用*指针。

Anagram subject;

You may also build a temporary Anagram object so you don't need to define the subject variable : 您还可以构建一个临时Anagram对象,因此不需要定义subject变量:

auto matches = Anagram{"diaper"}.matches({"hello", "world", "zombies","pants"});

If you want to use dynamic allocation, you could use new , but there isn't any GC to delete the value when you don't need anymore...! 如果要使用动态分配,则可以使用new ,但是当您不再需要该值时,没有任何GC可以删除该值...!

Anagram* subject = new Anagram("diaper");
// ... do some stuff with subject...
delete subject; // Destroy the dynamically allocated subject and deallocate it

If you ever want to use dynamic memory allocation, I would recommend you to use smart pointers which you can find on this cpprefrence page . 如果您想使用动态内存分配,我建议您使用可以在此cpprefrence页上找到的智能指针。 They have many advantages over manual memory allocation. 与手动分配内存相比,它们具有许多优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM