The C + + constructor uses the form ": member variable (formal parameter)" To assign a value to a member variable in a class, and if the member variable and the formal parameter are pointers, then the matters needing attention __jquery

Source: Internet
Author: User

Let me first list the conclusions:

When member variables and parameters are pointers, it is best not to use a form such as a member variable (formal parameter). Because you can not do this: a member variable = an assignment in this direction of the parameter, you may be performing: the assignment of a parameter = member variable in this direction. Because of the premise, they are all pointers. today, I came across a mistake like this:

The following program, the compilation is normal, but the operation is not. (I just posted the relevant code)

Class pclview{public
:
    pclview (pcl::visualization::P clvisualizer * &p);
    void Showcloudsleft (const PCL::P OINTCLOUD<PCL::P ointxyz>::P TR &cloud_source,
                        const PCL::P Ointcloud <PCL::P ointxyz>::P tr &cloud_target);
Private:
    pcl::visualization::P clvisualizer *p;
Pclview::P clview (pcl::visualization::P clvisualizer * &p):p (p) {vp_1 = 1;
    vp_2 = 2; Create a Pclvisualizer object p = new pcl::visualization::P clvisualizer ("pairwise incremental Registration
    ");
    P->createviewport (0.0, 0, 0.5, 1.0, vp_1);
    P->createviewport (0.5, 0, 1.0, 1.0, vp_2);
P->removepointcloud ("Vp1_target");
                             } void Pclview::showcloudsleft (const PCL::P OINTCLOUD&LT;PCL::P ointxyz>::P tr &cloud_source,
  Const PCL::P OINTCLOUD&LT;PCL::P ointxyz&gt::P tr &cloud_target) {p->removepointcloud ("Vp1_target");

  P->removepointcloud ("Vp1_source");
  Pcl::visualization::P ointcloudcolorhandlercustom<pcl::P ointxyz> tgt_h (cloud_target, 0, 255, 0);
  Pcl::visualization::P ointcloudcolorhandlercustom<pcl::P ointxyz> src_h (cloud_source, 255, 0, 0);
  P->addpointcloud (Cloud_target, Tgt_h, "Vp1_target", vp_1);

P->addpointcloud (Cloud_source, Src_h, "Vp1_source", vp_1); Pcl_iNFO ("Press spaces to begin the registration.\n");
P-> spinonce ();
 }

When we define an instantiated object for the above class in the Main.cpp file, we then call the Showcloudsleft () method of the class. There is no problem with compiling the program, but there is an error running the program.

int main (void) {
    pcl::visualization::P clvisualizer *p;
    Pclview Viewer (p);
    PCL::P OINTCLOUD<PCL::P ointxyz>::P tr &cloud_source;
    PCL::P OINTCLOUD<PCL::P ointxyz>::P tr &cloud_target;
    Viewer.showcloudsleft (Cloud_source, cloud_target);
    return 0;
}

The program will die in the P->removepointcloud ("Vp1_target") inside the Showcloudsleft () function when the above program runs; The reason is: the P pointer is empty. the correct wording:

The problem arises in the Pclview::P clview (pcl::visualization::P clvisualizer * &p) This constructor.

There are two types of correct writing:

Pclview::P clview (pcl::visualization::P Clvisualizer * &p) {
    this->p = p;
    Vp_1 = 1;
    vp_2 = 2;
    Create a Pclvisualizer object
    this->p = new pcl::visualization::P clvisualizer ("pairwise incremental Registration Example ");
    This->p->createviewport (0.0, 0, 0.5, 1.0, vp_1);
    This->p->createviewport (0.5, 0, 1.0, 1.0, vp_2);
    This->p->removepointcloud ("Vp1_target");
}

Or:

Pclview::P clview (pcl::visualization::P clvisualizer * &p):p (p) {
    vp_1 = 1;
    vp_2 = 2;
    Create a Pclvisualizer object
    this->p = new pcl::visualization::P clvisualizer ("pairwise incremental Registration Example ");
    This->p->createviewport (0.0, 0, 0.5, 1.0, vp_1);
    This->p->createviewport (0.5, 0, 1.0, 1.0, vp_2);
    This->p->removepointcloud ("Vp1_target");
}

Now the problem is solved.

But while the problem is solved now, there are potential problems with the program. (The current procedures are not yet up to the goal we want.) We started by designing this constructor to pcl::visualization the pointer that was passed in externally through the parameter to the instantiated object::P Clvisualizer * p pointers can point to the same thing, and then all two of them can change it.

But now we're not doing that. We can use the following code to verify:

int main (void) {
    pcl::visualization::P clvisualizer *p;
    Pclview Viewer (p);
    PCL::P OINTCLOUD<PCL::P ointxyz>::P tr &cloud_source;
    PCL::P OINTCLOUD<PCL::P ointxyz>::P tr &cloud_target;
    Viewer.showcloudsleft (Cloud_source, cloud_target);
    P->removepointcloud ("Vp1_target");
    return 0;
}

There is no problem with the compiler, but now it runs, the program is P->removepointcloud ("Vp1_target") inside the main () function;

So, the correct solution is:

Pclview::P clview (pcl::visualization::P Clvisualizer * &p) {
    vp_1 = 1;
    vp_2 = 2;
    Create a Pclvisualizer object
    this->p = new pcl::visualization::P clvisualizer ("pairwise incremental Registration Example ");
    This->p->createviewport (0.0, 0, 0.5, 1.0, vp_1);
    This->p->createviewport (0.5, 0, 1.0, 1.0, vp_2);
    This->p->removepointcloud ("Vp1_target");
    p = this->p;
}

Now it becomes the perfect program, there is no problem, there is no potential problem. the reason for the above problem arises

The reason is simple, the perfect design idea is: Want two pointers all point to the same storage space, but, in fact, two pointers do not point to a storage space, one pointer to storage space, and one is a null pointer, so there is an error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.