How to call the constructor like a function?

constructor function javascript
javascript class constructor
javascript constructor prototype
constructor function c++
constructor java
javascript constructor es6
constructor function python
object function

I am trying to create a temporary C++ object for the same of quick invocation in the constructor and nothing else.

For some reason, that's not working

Here's the code:

typedef std::vector<std::string> string_vec_t;

//-------------------------------------------------------------------------
struct visit_items
{
    virtual void action(std::string *s) = 0;
};

void items_visitor(visit_items *v)
{
    string_vec_t l = { "1", "2", "3", "4" };
    for (auto &s: l)
        v->action(&s);
}

struct my_visit_items: visit_items
{
public:
    string_vec_t *r;
    my_visit_items(string_vec_t *r /*, int */): r(r)
    {
        items_visitor(this);
    }

    virtual void action(std::string *s) override
    {
        r->push_back(*s);
        printf("visited: %s\n", s->c_str());
    }
};

//-------------------------------------------------------------------------
int main(int argc)
{
    string_vec_t r;
    r.push_back("1");
    my_visit_items(&r /*, 0 */);

    return -1;
}

If I have the ctor take an extra argument, then no problem. But as it stand, it does not work and I don't understand the error:

1>error C2040: 'r': 'my_visit_items &' differs in levels of indirection from 'string_vec_t'
1>error C2530: 'r': references must be initialized

my_visit_items(&r /*, 0 */); is a declaration of the my_visit_items reference r, just like my_visit_items &r; would have been. Try uniform Initialization instead. You should also try to avoid passing pointers to strings and vectors around. The code you showed would work fine without pointers:

#include <iostream>
#include <vector>
#include <string>

typedef std::vector<std::string> string_vec_t;

//-------------------------------------------------------------------------
struct visit_items {
    virtual void action(const std::string& s) = 0;
    virtual ~visit_items() = default;
};

void items_visitor(visit_items& v) {
    string_vec_t l = {"1", "2", "3", "4"};
    for(auto& s : l) v.action(s);
}

struct my_visit_items : visit_items {
    string_vec_t& r;
    my_visit_items(string_vec_t& r) :
        visit_items{},
        r(r)
    {
        items_visitor(*this);
    }

    virtual void action(const std::string& s) override {
        r.push_back(s);
        std::cout << "Visited: " << s << "\n";
    }
};

//-------------------------------------------------------------------------
int main() {
    string_vec_t r;
    r.push_back("1");
    my_visit_items{r};    //   { } instead of ( )

    return 1;
}

How to call the constructor like a function?, The this keyword in Java is a reference to the object of the current class. Using it, you can refer a field, method or, constructor of a class. Therefore, if you need to invoke a constructor explicitly you can do so, using "this()". You can't call constructors like this. What your code does is create a new temporary instance of multi, which gets discarded immediately. Once an object is constructed, you can't call its constructor again. Create an assign() function or something similar in your class.

When you want to pass a reference then you need to call do_this(r); With calling do_this(&r) you are passing a pointer to r.

Further you can call the constructor (theoretically) directly with do_this::do_this(r);

But why not using a function when you want to have a function? Not every compiler allows this without setting some compiler options explicitly (e.g., gcc requires -fpersmissive). If the reason is that you want to implement your function only once, then call the function from the constructor. If you want to have a function that is encapsulated in class without requiring attributes of the class, you can also use a static class member function.

Can we call a constructor directly from a method in java?, Destructor should not be called explicitly when the object is dynamically allocated because delete operator automatically calls destructor. As an� You call a constructor when you want to create a new object instance. For example if you have a class Button, then you call the constructor if you want to create a new Button instance. If you don't need a new object, then don't use a constructor.

Unrelated to errors.

On my Linux, g++-v7:

If you turn on enough compiler flags, you might discover that the compiler is warning about this struct:

struct visit_items
{
    virtual void action(std::string *s) = 0;
};

with

// warning: ‘struct visit_items’ has virtual functions and 
// accessible non-virtual destructor [-Wnon-virtual-dtor]

Note: This warning triggers 2 other issues (where the class is used).

This all clears up when you provide a virtual default dtor

struct visit_items  
{
   virtual ~visit_items() = default;
   virtual void action(std::string *s) = 0;
};

Is it possible to call constructor and destructor explicitly , In Javascript, that built-in init function is called a constructor function, and we can invoke it by using the special Javascript keyword new . this. type = type; this. The Class Constructor. A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

Use Constructor Functions, The __init__ method (often called "the initialiser") is passed the newly created instance as an argument (conventionally called " self "). Object Pascal constructors� The syntax exists like that for a reason. If you are dead set on that notation, I would just write a wrapper method in the component you're using.

Constructor (object-oriented programming), In your example you just use the constructor as a convoluted way of calling a method, but you don't actually use the created object (the result of� The first way is to implement a separate initialization function, and call it from both constructors. We’ll spare you the examples – it should be obvious as it is. That’s a fine, reliable, clear, and safe technique. However, some bad programmers want to make their code even shorter.

When to call the constructor and when to call the method in Java , As a general rule, you should never call virtual functions in constructors or destructors. If you do, those calls will never go to a more derived class than the� The Function () constructor expects any number of string arguments. The last argument is the body of the function – it can contain arbitrary JavaScript statements, separated from each other by semicolons. Notice that the Function () constructor is not passed any argument that specifies a name for the function it creates.

Comments
  • Why don't you write a function?
  • By calling do_this(&r); you are passing a string_vec_t*, not a string_vec_t&
  • do_this(r); for calling the reference version. Remove the addressof operator.
  • I am sorry, the example is wrong. ``` struct do_this: do_what { public: string_vec_t *r; do_this(string_vec_t *r): r(r) { } virtual void action() override { } }; ```
  • @BenS If it's wrong, please edit the question, don't just leave a comment. You should also elaborate on precisely what you mean by 'for some reason, that's not working'.
  • Thank you Ted. That was the problem!
  • Thanks for your answer, I had a wrong code snippet and I have updated it accordingly. Please delete your answer and my comments to better reflect this topic.