Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 665483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:42:47+00:00 2026-05-13T23:42:47+00:00

I have a class with an object as a member which doesn’t have a

  • 0

I have a class with an object as a member which doesn’t have a default constructor. I’d like to initialize this member in the constructor, but it seems that in C++ I can’t do that. Here is the class:

#include <boost/asio.hpp>
#include <boost/array.hpp>

using boost::asio::ip::udp;

template<class T>
class udp_sock
{
    public:
        udp_sock(std::string host, unsigned short port);
    private:
        boost::asio::io_service _io_service;
        udp::socket _sock;
        boost::array<T,256> _buf;
};

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost",
  unsigned short port = 50000)
{
    udp::resolver res(_io_service);
    udp::resolver::query query(udp::v4(), host, "spec");
    udp::endpoint ep = *res.resolve(query);
    ep.port(port);
    _sock(_io_service, ep);
}

The compiler tells me basically that it can’t find a default constructor for udp::socket and by my research I understood that C++ implicitly initializes every member before calling the constructor. Is there any way to do it the way I wanted to do it, or is it too “Java-oriented” and not feasible in C++?

I worked around the problem by defining my constructor like this:

template<class T>
udp_sock<T>::udp_sock(std::string host = "localhost",
  unsigned short port = 50000) : _sock(_io_service)
{
    udp::resolver res(_io_service);
    udp::resolver::query query(udp::v4(), host, "spec");
    udp::endpoint ep = *res.resolve(query);
    ep.port(port);
    _sock.bind(ep);
}

So my question is more out of curiosity and to better understand OOP in C++

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:42:47+00:00Added an answer on May 13, 2026 at 11:42 pm

    When you define a constructor, you have 2 ways to “initialize” attributes:

    • the initializer list
    • the constructor body

    If you do not explictly initialize one of the attributes in the initializer list, it is nonetheless initialized (by calling its default constructor) for you…

    So in essence:

    class Example
    {
    public:
      Example();
    private:
      Bar mAttr;
    };
    
    // You write
    Example::Example() {}
    
    // The compiler understands
    Example::Example(): mAttr() {}
    

    And this of course fails if the underlying type does not have a Default Constructor.

    There are various ways to defer this initialization. The “standard” way would be to use a pointer:

    class Example { public: Example(); private: Bar* mAttr; };
    

    However I prefer using Boost.Optional combined with suitable accessors:

    class Example
    {
    public: Example();
    private:
      Bar& accessAttr() { return *mAttr; }
      const Bar& getAttr() const { return *mAttr; }
      boost::Optional<Bar> mAttr;
    };
    
    Example::Example() { mAttr = Bar(42); }
    

    Because Boost.Optional means that there is no overhead on the allocation and no overhead on the dereferencing (the object is created in place) and yet carries the correct semantic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 340k
  • Answers 340k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can grab the element using document.getElementById and manipulate it… May 14, 2026 at 4:41 am
  • Editorial Team
    Editorial Team added an answer If the types don't have relations the answer is simple:… May 14, 2026 at 4:41 am
  • Editorial Team
    Editorial Team added an answer I dont know about optmizing but to order your array… May 14, 2026 at 4:41 am

Related Questions

Suppose I have a class module clsMyClass with an object as a member variable.
It seems as though different instances of a class can know about each others'
I need to copy an object that has a pretty deep hierarchy of member
Is their a way to use a non-member non-friend function on an object using
This is a bit of a strange one, but I've been struggling for a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.