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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:08:36+00:00 2026-05-19T02:08:36+00:00

I have a Polynomial class which holds coefficients of a given polynomial. One of

  • 0

I have a Polynomial class which holds coefficients of a given polynomial.
One of its overloaded constructors is supposed to receive these coefficients via variable argument list.

template <class T>
Polynomial<T>::Polynomial(T FirstCoefficient, ...)
{
    va_list ArgumentPointer;
    va_start(ArgumentPointer, FirstCoefficient);
    T NextCoefficient = FirstCoefficient;
    std::vector<T> Coefficients;
    while (true)
    {
        Coefficients.push_back(NextCoefficient);
        NextCoefficient = va_arg(ArgumentPointer, T);
        if (/* did we retrieve all the arguments */) // How do I implement this?
        {
            break;
        }
    }
    m_Coefficients = Coefficients;
}

I know that we usually pass an extra parameter which tells the recipient method the total number of parameters, or we pass a sentimental ending parameter. But in order to keep thing short and clean, I don’t prefer passing an extra parameter.

Is there any way of doing this without modifying the method signature in the example?

  • 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-19T02:08:37+00:00Added an answer on May 19, 2026 at 2:08 am

    In general, no, there’s no way to know when you’ve hit the last argument and you’ll need to provide some sort of sentinel. Variable-length argument lists are inherently unsafe for this reason; there’s no way to statically check that all the arguments have been received correctly, or that they have the right types, etc. If you take a look at most varargs functions, they all have some way of knowing how many arguments there are. printf and scanf use the formatting string, open looks at the type of operation, etc.

    In C++0x, however, you’ll be able to do this using std::initializer_list, which is a smarter, more type-safe alternative to varargs. For example:

    template <class T>
    Polynomial<T>::Polynomial(T FirstCoefficient, std::initializer_list<T> coeffs)
    {
        m_coefficients.insert(m_coefficients.begin(), coeffs.begin(), coeffs.end());
    }
    

    The compiler can now type-check that all the arguments have type T and will do all the range-checking for you.

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

Sidebar

Related Questions

I have a polynomial class similar to one here: Polynomial.java . Except I haven't
I am working on a Polynomial class which uses the STL linked list. One
I have written a polynomial class along the lines described in SICP 2.5.3 (except
I have a function which evaluates terms of a polynomial in several variables. The
have written this little class, which generates a UUID every time an object of
Have deployed numerous report parts which reference the same view however one of them
I have code that looks like this: public class Polynomial { List<Term> term =
I have a the generator polynomial which has to be converted to binary number
So I have been developing a polynomial class where a user inputs: 1x^0 +
I designing a polynomial class for one of my com sci courses , I

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.