Solved

How do I pass argv to a class

Posted on 2001-06-20
4
486 Views
Last Modified: 2010-04-02
I want to be able to pass the argv parameter given within the main() function of an app to a class, and assign it to a class member, such as :-

// from main

main(int argc, char* argv[])
{
   CMyClass myClass;

   myClass.Init(argc, argv);

......

}

// in myClass

CMyClass::Init(int argc, char* argv[])
{
   m_nArgc = argc;

  m_szArgv = argv;     // is this correct;

}

What I would like to know, is what data type must m_szArgv be declared as and how should argv be assigned to it?
0
Comment
Question by:carlpaddick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 6209896
m_szArgv should be a char **, and the way you assign it is fine.  If you don't want to mess with char **'s, you might want to look at assigning argv to a vector of strings, like:

class CMyClass
{
...
  std::vector<std::string> m_Argv; // Could also be CString
};

// in myClass

CMyClass::Init(int argc, char* argv[])
{
  for(int i = 0; i < argc; ++i)
    m_Argv.push_back(argv[i]);
}
And you can still use the subscript operator to get data out later.  e.g. std::string param1 = m_Argv[0];

I hope this helps you.
0
 

Author Comment

by:carlpaddick
ID: 6209938
Thanks mnashadka,

Good answer.  Please have the points.

Keep up the good work.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6222886
Does anyone know if this accepted answer was posted as a comment or as an answer?
0
 

Author Comment

by:carlpaddick
ID: 6223717
nietod,

Again, I posted this comment as an accepted answer.

Thanks,

Carl
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question