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

  • SEARCH
  • Home
  • 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

Related Questions

I have a class that contains a member object. I would like to call
I have a class member in an ActionScript 3 class that looks something like
Suppose I have a class module clsMyClass with an object as a member variable.
I have a base class object array into which I have typecasted many different
I have a python module that defines a number of classes: class A(object): def
I have a MATLAB class which contains a reference to a java object classdef
I have this class in Scala: object Util { class Tapper[A](tapMe: A) { def
I have a common code of serializing a class object in my 3-4 methods
I have the following example code: class A(object): def __init__(self, id): self.myid = id
Basically, I have a class with 2 methods: one to serialize an object into

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.