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 1928294
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:57:39+00:00 2026-05-17T06:57:39+00:00

I have a class like this: template <class T> class bag { private: typedef

  • 0

I have a class like this:

template <class T>
    class bag
    {
    private:    
        typedef struct{T item; unsigned int count;} body;
        typedef struct _node{_node* prev; body _body; _node* next;}* node;  
        struct iterator{
            enum exception{NOTDEFINED, OUTOFLIST};
            body operator*();

            explicit iterator();
            explicit iterator(const iterator&);
            iterator& operator=(const iterator&);
            iterator& operator++(int);
            iterator& operator--(int);
            bool operator==(const iterator&) const;
            bool operator!() const;     
        private:
            node current;

            friend class bag;
        };
        node head;
        node foot;

        iterator _begin;
        iterator _end;

        /* ... */

    public: /* ... */
        bag();
        const iterator& begin;
        const iterator& end;        
    };

In the bag() I have to set the reference begin to _begin, and end to _end.

begin = _begin;
end = _end;

But I think this line

begin = _begin;

invokes bag::iterator::operator=() function.

How can i avoid that?

  • 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-17T06:57:39+00:00Added an answer on May 17, 2026 at 6:57 am

    References can’t be assigned, only initialised. So you will need to initialise them in the constructor’s initialisation list:

    bag() : begin(_begin), end(_end) {}
    

    However, it’s more conventional (and also reduces the class size) to get these using accessor functions rather than public references:

    const iterator& begin() {return _begin;}
    const iterator& end() {return _end;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I can have a typedef function pointer in a class like this: template<typename T>
I have a class like this: struct WorkItemResultData; template <typename ExecutionPolicy> class Engine {
I want to do something like this: template<typename CType, unsigned int targetDimensions> struct GeneratePointerType
I have a C++ class like this: class ConnectionBase { public: ConnectionBase(); template <class
I have a templated container class that looks something like this: template <class ItemType>
I have code that looks like this: template<class T> class list { public: class
Suppose I have a function which looks like this: template <class In, class In2>
I have a class like this public class foo { private void getThread() {
Suppose I have an autolocker class which looks something like this: template <T> class

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.